Initial commit of pristine erwise source
[erwise] / Cl / WWWLibrary / HTTP.c
1 /*      HyperText Tranfer Protocol      - Client implementation         HTTP.c
2 **      ==========================
3 */
4
5 /*      Module parameters:
6 **      -----------------
7 **
8 **  These may be undefined and redefined by syspec.h
9 */
10 #include "HTParse.h"
11 #include "HTUtils.h"
12 #include "tcp.h"
13 #include "HTTCP.h"
14 #include "HTFormat.h"
15
16
17 #ifndef ERWISE
18 /*              Load Dcoument from HTTP Server                  HTLoadHTTP()
19 **              ==============================
20 **
21 **      Given a hypertext address, this routine loads a document.
22 **
23 **
24 ** On entry,
25 **      arg     is the hypertext reference of the article to be loaded.
26 **      gate    is nill if no gateway, else the gateway address.
27 **
28 ** On exit,
29 **      returns >=0     If no error, a good socket number
30 **              <0      Error.
31 **
32 **      The socket must be closed by the caller after the document has been
33 **      read.
34 **
35 */
36 PUBLIC int HTLoadHTTP ARGS4 (CONST char *, arg,
37         CONST char *,   gate, 
38         HTAnchor *,     anAnchor,
39         int,            diag)
40 {
41     int s;                              /* Socket number for returned data */
42     char *command;                      /* The whole command */
43     int status;                         /* tcp return */
44  
45     struct sockaddr_in soc_address;     /* Binary network address */
46     struct sockaddr_in* sin = &soc_address;
47
48     if (!arg) return -3;                        /* Bad if no name sepcified     */
49     if (!*arg) return -2;                       /* Bad if name had zero length  */
50
51 /*  Set up defaults:
52 */
53     sin->sin_family = AF_INET;                  /* Family, host order  */
54     sin->sin_port = htons(TCP_PORT);            /* Default: new port,    */
55
56     if (TRACE) {
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);
60     }
61     
62 /* Get node name and optional port number:
63 */
64     {
65         char *p1 = HTParse(gate ? gate : arg, "", PARSE_HOST);
66         HTParseInet(sin, p1);
67         free(p1);
68     }
69     
70    
71 /*      Now, let's get a socket set up from the server for the sgml data:
72 */      
73     s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
74     status = connect(s, (struct sockaddr*)&soc_address, sizeof(soc_address));
75     if (status<0){
76         if (TRACE) printf(
77             "HTTP: Unable to connect to remote host for `%s'.\n",
78             arg);
79         free(command);
80         return HTInetStatus("connect");
81     }
82     
83     if (TRACE) printf("HTTP connected, socket %d\n", s);
84
85 /*      Ask that node for the document,
86 **      omitting the host name & anchor if not gatewayed.
87 */        
88     if (gate) {
89         command = malloc(4 + strlen(arg)+ 2 + 1);
90         if (command == NULL) outofmem(__FILE__, "HTLoadHTTP");
91         strcpy(command, "GET ");
92         strcat(command, arg);
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 ");
98         strcat(command, p1);
99         free(p1);
100     }
101     strcat(command, "\r\n");            /* Include CR for telnet compat. */
102             
103
104     if (TRACE) printf("HTTP writing command `%s' to socket %d\n", command, s);
105     
106 #ifdef NOT_ASCII
107     {
108         char * p;
109         for(p = command; *p; p++) {
110             *p = TOASCII(*p);
111         }
112     }
113 #endif
114
115     status = NETWRITE(s, command, (int)strlen(command));
116     free(command);
117     if (status<0){
118         if (TRACE) printf("HTTPAccess: Unable to send command.\n");
119             return HTInetStatus("send");
120     }
121
122 /*      Now load the  date
123 */
124     HTParseFormat(diag ? WWW_PLAINTEXT : WWW_HTML,
125                 (HTParentAnchor *) anAnchor, s);
126     
127     if (TRACE) printf("HTTP: close socket %d.\n", s);
128     status = NETCLOSE(s);
129
130     return HT_LOADED;                   /* Good return */
131 }
132 #endif /* ERWISE */