URL 22-SEP-1995 18:31:43 VAX C V3.2-044 Page 1 V1.0 6-FEB-1995 16:10:48 [GOPHER.G2.VMS2_13.OBJECT]URL.C;1 (1) 1 /******************************************************************** 2 * lindner 3 * 3.16 4 * 1995/02/06 22:10:47 5 * /home/arcwelder/GopherSrc/CVS/gopher+/object/url.c,v 6 * Exp 7 * 8 * Paul Lindner, University of Minnesota CIS. 9 * 10 * Copyright 1991, 1992 by the Regents of the University of Minnesota 11 * see the file "Copyright" in the distribution for conditions of use. 12 ********************************************************************* 13 * MODULE: url.c 14 * Simplified method of getting urls.. 15 ********************************************************************* 16 * Revision History: 17 * url.c,v 18 * Revision 3.16 1995/02/06 22:10:47 lindner 19 * Fix for systems without strstr, compiler casts 20 * 21 * Revision 3.15 1994/12/05 22:47:19 lindner 22 * Fixed settings of Gopher Type in URLparse() 23 * 24 * Revision 3.14 1994/11/18 22:45:55 lindner 25 * Remove unused variable 26 * 27 * Revision 3.13 1994/11/18 21:41:48 lindner 28 * Fix order of FTP arguments 29 * 30 * Revision 3.12 1994/11/13 06:30:06 lindner 31 * Expanded URL parsing routines 32 * 33 * Revision 3.11 1994/10/13 05:25:56 lindner 34 * Compiler complaint fixes 35 * 36 * Revision 3.10 1994/08/19 16:28:30 lindner 37 * More hexification of URL strings 38 * 39 * Revision 3.9 1994/07/25 03:51:41 lindner 40 * Compiler fix 41 * 42 * Revision 3.8 1994/07/21 22:17:20 lindner 43 * Add ftp urls 44 * 45 * Revision 3.7 1994/03/04 17:42:57 lindner 46 * Fixes from Alan Coopersmith 47 * 48 * Revision 3.6 1994/01/21 04:25:41 lindner 49 * Add support for tn3270 and better gopher url handling 50 * 51 * Revision 3.5 1993/12/30 04:18:08 lindner 52 * translate telnet url correctly 53 * 54 * Revision 3.4 1993/12/27 16:14:03 lindner 55 * Enlarge buffer size, make html docs on gopher server into http: refs URL 22-SEP-1995 18:31:43 VAX C V3.2-044 Page 2 V1.0 6-FEB-1995 16:10:48 [GOPHER.G2.VMS2_13.OBJECT]URL.C;1 (1) 56 * 57 * Revision 3.3 1993/11/02 06:14:09 lindner 58 * Add url html hack 59 * 60 * 61 *********************************************************************/ 62 63 #include "url.h" 223 #include "GSgopherobj.h" 2891 #include "Malloc.h" 3549 #include "String.h" 4066 #include "compatible.h" 4449 4450 Url * 4451 URLnew() 4452 { 4453 1 Url *temp; 4454 1 4455 1 temp = (Url *) malloc(sizeof(Url)); 4456 1 temp->url = STRnew(); 4457 1 temp->Host = STRnew(); 4458 1 temp->Port = -1; 4459 1 temp->Path = STRnew(); 4460 1 temp->User = STRnew(); 4461 1 temp->Pass = STRnew(); 4462 1 temp->Gtype = '\0'; 4463 1 4464 1 URLsetService(temp, unset); 4465 1 return(temp); 4466 1 } 4467 4468 4469 void 4470 URLdestroy(url) 4471 Url *url; 4472 { 4473 1 STRdestroy(url->url); 4474 1 STRdestroy(url->Host); 4475 1 STRdestroy(url->Path); 4476 1 STRdestroy(url->User); 4477 1 STRdestroy(url->Pass); 4478 1 4479 1 free(url); 4480 1 } 4481 4482 4483 4484 /* 4485 * Make a default url from the current Gopher Object... 4486 */ 4487 4488 void 4489 URLfromGS(url, gs) 4490 Url *url; 4491 GopherObj *gs; 4492 { 4493 1 char u[2048], *path, *cp; URL 22-SEP-1995 18:31:43 VAX C V3.2-044 Page 3 V1.0 6-FEB-1995 16:10:48 [GOPHER.G2.VMS2_13.OBJECT]URL.C;1 (1) 4494 1 4495 1 *u = '\0'; 4496 1 path = GSgetPath(gs); 4497 1 4498 1 if (GSgetHost(gs) == NULL) 4499 1 return; 4500 1 4501 1 if ((GSgetType(gs) == A_TN3270) || (GSgetType(gs) == A_TELNET)) { 4502 2 /* [telnet|tn3270]://[host]@[login]:[port] */ 4503 2 if (GSgetType(gs) == A_TELNET) 4504 2 strcpy(u,"telnet://"); 4505 2 else 4506 2 strcpy(u,"tn3270://"); 4507 2 Tohexstr(GSgetHost(gs), u + 9); 4508 2 if ( (path != NULL) && (*path != '\0') ) { 4509 3 cp = u + strlen(u); 4510 3 *(cp++) = '@'; 4511 3 Tohexstr(path, cp); 4512 3 } 4513 2 sprintf(u + strlen(u), ":%d", GSgetPort(gs)); 4514 2 } 4515 1 else if (path != NULL) 4516 1 { 4517 2 if (strncmp(path, "GET /", 5) == 0) { 4518 3 /* http://[host]:[port][path without "GET "] */ 4519 3 strcpy(u, "http://"); 4520 3 Tohexstr(GSgetHost(gs), u+7); 4521 3 sprintf(u + strlen(u), ":%d", GSgetPort(gs)); 4522 3 Tohexstr(path + 4, u + strlen(u)); 4523 3 } else if (strncmp(path, "ftp:", 4) == 0) { 4524 3 /** ftp://[host]/[pathname]; **/ 4525 3 char *atsign; 4526 3 4527 3 strcpy(u, "ftp://"); 4528 3 strcat(u, path + 4); 4529 3 atsign = strchr(u, '@'); 4530 3 if (atsign != NULL) 4531 3 Tohexstr( path + (atsign-u)-1, atsign ); 4532 3 } else { 4533 3 /* gopher://[host]:[port]/[type][path] */ 4534 3 strcpy(u, "gopher://"); 4535 3 Tohexstr(GSgetHost(gs), u + 9); 4536 3 sprintf(u + strlen(u), ":%d/%c", GSgetPort(gs), GSgetType(gs)); 4537 3 Tohexstr(path, u + strlen(u)); 4538 3 } 4539 2 } 4540 1 URLset(url, u); 4541 1 } 4542 4543 /* 4544 * Hack gopher directories into an HTML type... 4545 */ 4546 4547 void 4548 URLmakeHTML(url) 4549 Url *url; 4550 { URL 22-SEP-1995 18:31:43 VAX C V3.2-044 Page 4 V1.0 6-FEB-1995 16:10:48 [GOPHER.G2.VMS2_13.OBJECT]URL.C;1 (1) 4551 1 char *cp = URLget(url); 4552 1 4553 1 if (cp == NULL) 4554 1 return; 4555 1 4556 1 if (strncmp(cp, "gopher://", 9) != 0) 4557 1 return; 4558 1 4559 1 /** find the type character **/ 4560 1 cp = strchr(cp+10, '/'); 4561 1 4562 1 if (cp ==NULL) 4563 1 return; 4564 1 4565 1 /** Test link for current host **/ 4566 1 /* host = cp+10; 4567 1 if (strcasecmp(host, hostname) != 0) 4568 1 return;*/ 4569 1 4570 1 cp ++; 4571 1 /** cp is now pointed at the type character **/ 4572 1 4573 1 if (*cp == '1' && *(cp+1) == '1') { 4574 2 /** It's a directory **/ 4575 2 *cp = 'h'; 4576 2 *(cp+1) = 'h'; 4577 2 } 4578 1 } 4579 4580 4581 /* 4582 * Get the transport of the specified URL 4583 */ 4584 4585 UrlServiceType 4586 URLgetService(url) 4587 Url *url; 4588 { 4589 1 UrlServiceType st; 4590 1 4591 1 if (url->service == unset) { 4592 2 char *urlcp; 4593 2 4594 2 urlcp = URLget(url); 4595 2 4596 2 if (urlcp == NULL) 4597 2 return(unknown); 4598 2 4599 2 st = unknown; 4600 2 4601 2 if (strncasecmp("ftp:", urlcp, 4)==0) 4602 2 st = ftp; 4603 2 else if (strncasecmp(urlcp, "gopher:", 7)==0) 4604 2 st = gopher; 4605 2 else if (strncasecmp(urlcp, "telnet:", 7)==0) 4606 2 st = telnet; 4607 2 else if (strncasecmp(urlcp, "tn3270:", 7)==0) URL 22-SEP-1995 18:31:43 VAX C V3.2-044 Page 5 V1.0 6-FEB-1995 16:10:48 [GOPHER.G2.VMS2_13.OBJECT]URL.C;1 (1) 4608 2 st = tn3270; 4609 2 else if (strncasecmp(urlcp, "http:", 5)==0) 4610 2 st = http; 4611 2 4612 2 URLsetService(url, st); 4613 2 } 4614 1 return(url->service); 4615 1 } 4616 4617 static boolean 4618 URLparse(url) 4619 Url *url; 4620 { 4621 1 char *cp, *cp2, *cp3, *urlcp; 4622 1 UrlServiceType serviceType; 4623 1 4624 1 serviceType = URLgetService(url); 4625 1 4626 1 if (serviceType == unknown) 4627 1 return(FALSE); 4628 1 4629 1 urlcp = URLget(url); 4630 1 4631 1 if (urlcp == NULL) 4632 1 return(FALSE); 4633 1 4634 1 urlcp = strdup(urlcp); /* This lets us scribble on the string */ 4635 1 4636 1 cp = strstr(urlcp, "://"); /* skip over servicetype + "://" */ 4637 1 4638 1 if (cp == NULL) 4639 1 return(FALSE); 4640 1 4641 1 cp += 3; 4642 1 4643 1 cp3 = strchr(cp, '/'); /* end of login:passwd@host:port/ section */ 4644 1 4645 1 if (cp3 == NULL) { 4646 2 cp3 = cp + strlen(cp) + 1; /* No slash at the end... */ 4647 2 URLsetPath(url, "\0"); 4648 2 } else { 4649 2 *cp3 = '\0'; 4650 2 cp3++; 4651 2 Fromhexstr(cp3, cp3); 4652 2 URLsetPath(url,cp3); 4653 2 } 4654 1 4655 1 if (serviceType == gopher) { 4656 2 if (cp3 != NULL && (int)strlen(cp3) > 1) { 4657 3 URLsetGophType(url, *(cp3++)); 4658 3 URLsetPath(url,cp3++); 4659 3 } 4660 2 else 4661 2 URLsetGophType(url, '1'); 4662 2 4663 2 } 4664 1 URL 22-SEP-1995 18:31:43 VAX C V3.2-044 Page 6 V1.0 6-FEB-1995 16:10:48 [GOPHER.G2.VMS2_13.OBJECT]URL.C;1 (1) 4665 1 4666 1 /* 4667 1 * Find Username/pass for ftp/telnet 4668 1 */ 4669 1 4670 1 cp2 = strchr(cp, '@'); 4671 1 if ((cp2 != NULL) && (cp2 < cp3)) { 4672 2 char *pass; 4673 2 4674 2 4675 2 *cp2 = '\0'; 4676 2 cp2++; 4677 2 4678 2 pass = strchr(cp, ':'); 4679 2 if ((pass != NULL) && (pass < cp3)) { 4680 3 *pass = '\0'; 4681 3 pass++; 4682 3 Fromhexstr(pass, pass); 4683 3 4684 3 URLsetPass(url, pass); 4685 3 4686 3 } 4687 2 URLsetUser(url, cp); 4688 2 4689 2 cp = cp2; /** cp now points to hostname **/ 4690 2 } 4691 1 4692 1 /* 4693 1 * Find port 4694 1 */ 4695 1 4696 1 cp2 = strchr(cp, ':'); 4697 1 4698 1 if ((cp2 != NULL) && (cp2 < cp3)) { 4699 2 *cp2 = '\0'; 4700 2 cp2++; 4701 2 URLsetPort(url, atoi(cp2)); 4702 2 URLsetHost(url, cp); 4703 2 } else { 4704 2 switch (serviceType) { 4705 3 case http: 4706 3 URLsetPort(url, 80); 4707 3 break; 4708 3 case telnet: 4709 3 case tn3270: 4710 3 URLsetPort(url, 23); 4711 3 break; 4712 3 case gopher: 4713 3 URLsetPort(url, 70); 4714 3 break; 4715 3 case news: 4716 3 URLsetPort(url, 119); 4717 3 break; 4718 3 } 4719 2 URLsetHost(url, cp); 4720 2 } 4721 1 URL 22-SEP-1995 18:31:43 VAX C V3.2-044 Page 7 V1.0 6-FEB-1995 16:10:48 [GOPHER.G2.VMS2_13.OBJECT]URL.C;1 (1) 4722 1 4723 1 return(TRUE); 4724 1 } 4725 4726 4727 4728 char * 4729 URLgetHost(url) 4730 Url *url; 4731 { 4732 1 if (STRget(url->Host) == NULL) 4733 1 URLparse(url); 4734 1 4735 1 return(STRget(url->Host)); 4736 1 } 4737 4738 int 4739 URLgetPort(url) 4740 Url *url; 4741 { 4742 1 if (url->Port == -1) 4743 1 URLparse(url); 4744 1 4745 1 return(url->Port); 4746 1 } 4747 4748 4749 char * 4750 URLgetPath(url) 4751 Url *url; 4752 { 4753 1 if (STRget(url->Path) == NULL) 4754 1 URLparse(url); 4755 1 return(STRget(url->Path)); 4756 1 } 4757 4758 char * 4759 URLgetUser(url) 4760 Url *url; 4761 { 4762 1 if (STRget(url->User) == NULL) 4763 1 URLparse(url); 4764 1 return(STRget(url->User)); 4765 1 } 4766 4767 char * 4768 URLgetPass(url) 4769 Url *url; 4770 { 4771 1 if (STRget(url->Pass) == NULL) 4772 1 URLparse(url); 4773 1 return(STRget(url->Pass)); 4774 1 } 4775 4776 4777 char 4778 URLgetGophType(url) URL 22-SEP-1995 18:31:43 VAX C V3.2-044 Page 8 V1.0 6-FEB-1995 16:10:48 [GOPHER.G2.VMS2_13.OBJECT]URL.C;1 (1) 4779 Url *url; 4780 { 4781 1 if (URLgetService(url) != gopher) 4782 1 return('\0'); 4783 1 4784 1 if (url->Gtype == '\0') 4785 1 URLparse(url); 4786 1 return(url->Gtype); 4787 1 } 4788 Command Line ------------ CC/INCL=([-],[-.OBJECT])/DEFINE=(MULTINET=1,DEBUGGING,__VMS)/DEBUG/NOOPT/OBJ=[.VAX.DBG]/LIS=[.VAX.LIS] URL.C .