1 /* HyperText Tranfer Protocol - Client implementation HTTP.c
2 ** ==========================
8 ** These may be undefined and redefined by syspec.h
18 /* Load Dcoument from HTTP Server HTLoadHTTP()
19 ** ==============================
21 ** Given a hypertext address, this routine loads a document.
25 ** arg is the hypertext reference of the article to be loaded.
26 ** gate is nill if no gateway, else the gateway address.
29 ** returns >=0 If no error, a good socket number
32 ** The socket must be closed by the caller after the document has been
36 PUBLIC int HTLoadHTTP ARGS4 (CONST char *, arg,
41 int s; /* Socket number for returned data */
42 char *command; /* The whole command */
43 int status; /* tcp return */
45 struct sockaddr_in soc_address; /* Binary network address */
46 struct sockaddr_in* sin = &soc_address;
48 if (!arg) return -3; /* Bad if no name sepcified */
49 if (!*arg) return -2; /* Bad if name had zero length */
53 sin->sin_family = AF_INET; /* Family, host order */
54 sin->sin_port = htons(TCP_PORT); /* Default: new port, */
57 if (gate) fprintf(stderr,
58 "HTTPAccess: Using gateway %s for %s\n", gate, arg);
59 else fprintf(stderr, "HTTPAccess: Direct access for %s\n", arg);
62 /* Get node name and optional port number:
65 char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
71 /* Now, let's get a socket set up from the server for the sgml data:
73 s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
74 status = connect(s, (struct sockaddr*)&soc_address, sizeof(soc_address));
77 "HTTP: Unable to connect to remote host for `%s'.\n",
80 return HTInetStatus("connect");
83 if (TRACE) printf("HTTP connected, socket %d\n", s);
85 /* Ask that node for the document,
86 ** omitting the host name & anchor if not gatewayed.
89 command = malloc(4 + strlen(arg)+ 2 + 1);
90 if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
91 strcpy(command, "GET ");
93 } else { /* not gatewayed */
94 char * p1 = HTParse(arg, "", PARSE_PATH|PARSE_PUNCTUATION);
95 command = malloc(4 + strlen(p1)+ 2 + 1);
96 if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
97 strcpy(command, "GET ");
101 strcat(command, "\r\n"); /* Include CR for telnet compat. */
104 if (TRACE) printf("HTTP writing command `%s' to socket %d\n", command, s);
109 for(p = command; *p; p++) {
115 status = NETWRITE(s, command, (int)strlen(command));
118 if (TRACE) printf("HTTPAccess: Unable to send command.\n");
119 return HTInetStatus("send");
124 HTParseFormat(diag ? WWW_PLAINTEXT : WWW_HTML,
125 (HTParentAnchor *) anAnchor, s);
127 if (TRACE) printf("HTTP: close socket %d.\n", s);
128 status = NETCLOSE(s);
130 return HT_LOADED; /* Good return */