Initial commit of pristine erwise source
[erwise] / Cl / ClHTTP.c
1 /*
2  * ClHTTP.c --
3  *
4  * Author: Teemu Rantanen <tvr@cs.hut.fi>
5  * Copyright (c) 1992 Teemu Rantanen
6  *                    All rights reserved
7  *
8  * Created: Fri Apr 17 23:43:01 1992 tvr
9  * Last modified: Mon May 11 23:29:10 1992 tvr
10  *
11  */
12
13 #include <stdio.h>
14 #include <errno.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17
18 #include "Cl.h"
19
20 #include "HTParse.h"
21 #include "HTFormat.h"
22 #include "HTAnchor.h"
23 #include "tcp.h"
24
25
26
27 /*
28  * We want that loading won't kill the whole f*king client.
29  *
30  * This is a kludge ;)
31  */
32
33 PUBLIC int HTLoadHTTP
34 ARGS4 (CONST char *, arg,
35        CONST char *, gate,
36        HTAnchor *, anAnchor,
37        int, diag)
38 {
39   /*
40    * Most code from old loading function
41    */
42
43   int s;
44   int status;
45   char *command;
46
47   struct sockaddr_in soc_address;       /* Binary network address */
48   struct sockaddr_in *sin = &soc_address;
49
50   if (!arg)
51     return -3;                  /* Bad if no name sepcified     */
52   if (!*arg)
53     return -2;                  /* Bad if name had zero length  */
54
55 /*  Set up defaults:
56 */
57   sin->sin_family = AF_INET;    /* Family, host order  */
58   sin->sin_port = htons (TCP_PORT);     /* Default: new port,    */
59
60   if (TRACE)
61     {
62       if (gate)
63         fprintf (stderr,
64                  "HTTPAccess: Using gateway %s for %s\n", gate, arg);
65       else
66         fprintf (stderr, "HTTPAccess: Direct access for %s\n", arg);
67     }
68
69 /* Get node name and optional port number:
70 */
71   {
72     char *p1 = HTParse (gate ? gate : arg, "", PARSE_HOST);
73     HTParseInet (sin, p1);
74     free (p1);
75   }
76
77
78 /*      Now, let's get a socket set up from the server for the sgml data:
79 */
80   s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
81   (void) fcntl (s, F_SETFL, O_NONBLOCK);
82   (void) fcntl (s, F_SETFL, FNDELAY);
83
84   /*
85    * Now. Do first attempt to connect.
86    */
87   status = erwise_connect (s,
88                            (struct sockaddr *) & soc_address,
89                            sizeof (soc_address));
90
91   if (s < 0)
92     {
93       /*
94        * Fail
95        */
96       /*free(command);*/
97       return -1;
98     }
99
100   if (TRACE)
101     printf ("HTTP connected, socket %d\n", s);
102
103 /*      Ask that node for the document,
104 **      omitting the host name & anchor if not gatewayed.
105 */
106   if (gate)
107     {
108       command = malloc (4 + strlen (arg) + 1 + 1);
109       strcpy (command, "GET ");
110       strcat (command, arg);
111     }
112   else
113     {                           /* not gatewayed */
114       char *p1 = HTParse (arg, "", PARSE_PATH | PARSE_PUNCTUATION);
115       command = malloc (4 + strlen (p1) + 1 + 1);
116       strcpy (command, "GET ");
117       strcat (command, p1);
118       free (p1);
119     }
120   strcat (command, "\r\n");     /* Include CR for telnet compat. */
121
122
123   if (TRACE)
124     printf ("HTTP writing command `%s' to socket %d\n", command, s);
125
126 #ifdef NOT_ASCII
127   {
128     char *p;
129     for (p = command; *p; p++)
130       {
131         *p = TOASCII (*p);
132       }
133   }
134 #endif
135
136   /*
137    * Ok. Everything is now set up. Set functionpointers so that
138    * the rest of loading works ok.
139    */
140
141   {
142     static void (*functions[]) () =
143     {
144       WWWErwiseConnect,
145       WWWErwiseSendCommand,
146       WWWErwiseSetSelect,
147       WWWErwiseReadData,
148       WWWErwiseSetPoll,
149       WWWErwiseTerminateIfLoadToFile,
150       WWWErwiseParse,
151       NULL,
152     };
153     WWWErwiseConnection->function = functions;
154   }
155
156   WWWErwiseConnection->command = command;
157
158   WWWErwiseConnection->diag = diag;
159
160   WWWErwiseConnection->anAnchor = anAnchor;
161
162   /*
163    * Cheat HTOpen()
164    */
165   return s;
166 }