1 /* proxy.c Copyright 2000 by Michael Temari All Rights Reserved */
15 #include <net/netlib.h>
17 #include <net/gen/in.h>
18 #include <net/gen/inet.h>
19 #include <net/gen/tcp.h>
20 #include <net/gen/tcp_io.h>
21 #include <net/gen/socket.h>
22 #include <net/gen/netdb.h>
29 _PROTOTYPE(static int connect
, (char *host
));
31 _PROTOTYPE(static int readline
, (char *p
, int len
));
33 _PROTOTYPE(static int sendout
, (int fd
, char *data
));
35 static int connect(host
)
38 nwio_tcpconf_t tcpconf
;
43 tcpport_t netport
= 0;
51 while(*p
&& *p
!= ':') p
++;
54 netport
= htons(atoi(p
));
57 if((hp
= gethostbyname(host
)) == (struct hostent
*)NULL
) {
58 fprintf(stderr
, "Unknown host %s!\n", host
);
61 memcpy((char *) &nethost
, (char *) hp
->h_addr
, hp
->h_length
);
63 /* Now, to which port must we connect? */
65 if((sp
= getservbyname("http", "tcp")) == (struct servent
*)NULL
) {
66 fprintf(stderr
, "HTTP port is unknown????\n");
71 /* Connect to the host */
72 if((tcp_device
= getenv("TCP_DEVICE")) == NULL
)
73 tcp_device
= TCP_DEVICE
;
75 if((netfd
= open(tcp_device
, O_RDWR
)) < 0) {
76 perror("httpget: opening tcp");
80 tcpconf
.nwtc_flags
= NWTC_LP_SEL
| NWTC_SET_RA
| NWTC_SET_RP
;
81 tcpconf
.nwtc_remaddr
= nethost
;
82 tcpconf
.nwtc_remport
= netport
;
84 s
= ioctl(netfd
, NWIOSTCPCONF
, &tcpconf
);
86 perror("httpget: NWIOSTCPCONF");
91 s
= ioctl(netfd
, NWIOGTCPCONF
, &tcpconf
);
93 perror("httpget: NWIOGTCPCONF");
98 tcpcopt
.nwtcl_flags
= 0;
102 s
= ioctl(netfd
, NWIOTCPCONN
, &tcpcopt
);
103 if(s
== -1 && errno
== EAGAIN
) {
112 perror("httpget: NWIOTCPCONN");
123 static int readline(p
, len
)
132 if(len
< 0) return(-1);
133 while(len
> 0 && (c
= getchar()) != EOF
) {
134 if(c
== '\n' && cr
) {
150 static int sendout(fd
, data
)
155 write(fd
, data
, strlen(data
));
156 write(fd
, "\r\n", 2);
157 if(dbglog
!= (FILE *)NULL
) {
158 fprintf(dbglog
, "REPLY: %s\n", data
);
166 struct http_request
*rq
;
167 struct http_reply
*rp
;
174 static char user
[256];
175 static char pass
[256];
184 if(tolower(*p
++) != 'h') bad
++;
185 if(tolower(*p
++) != 't') bad
++;
186 if(tolower(*p
++) != 't') bad
++;
187 if(tolower(*p
++) != 'p') bad
++;
188 if(tolower(*p
++) != ':') bad
++;
189 if(tolower(*p
++) != '/') bad
++;
190 if(tolower(*p
++) != '/') bad
++;
192 sprintf(buffer
, "HTTP/%d.%d 400 Bad Request",
193 rq
->vmajor
, rq
->vminor
);
196 sendout(1, "Proxy Request was not http:");
200 while(*p
&& *p
!= '/') p
++;
203 at
= strchr(host
, '@');
204 if(at
!= (char *)NULL
) {
207 while(*p
&& *p
!= ':') p
++;
220 sprintf(buffer
, "HTTP/%d.%d 400 Bad Request",
221 rq
->vmajor
, rq
->vminor
);
224 sendout(1, "Could not connect to host");
227 if(rq
->method
== HTTP_METHOD_GET
)
228 write(fd
, "GET ", 4); else
229 if(rq
->method
== HTTP_METHOD_POST
)
230 write(fd
, "POST ", 5);
233 write(fd
, url
, strlen(url
));
235 sprintf(buffer
, "HTTP/%d.%d", rq
->vmajor
, rq
->vminor
);
237 if(rq
->ifmodsince
!= -1) {
238 write(fd
, "If-Mod-Since: ", 14);
239 sendout(fd
, httpdate(&rq
->ifmodsince
));
242 sendout(fd
, "Content-Type: application/x-www-form-urlencoded");
243 sprintf(buffer
, "Content-Length: %lu", rq
->size
);
247 sprintf(buffer
, "Cookie: %s", rq
->cookie
);
251 sprintf(buffer
, "User-Agent: %s", rq
->useragent
);
255 sprintf(buffer
, "Host: %s", rq
->host
);
259 sprintf(buffer
, "Authorization: %s", rq
->wwwauth
);
262 sprintf(buffer
, "X-Forwarded-From: %s", rmthostaddr
);
266 if(stdlog
!= (FILE *)NULL
) {
267 fprintf(stdlog
, "%s %s %d %d ",
268 logdate((time_t *)NULL
), rmthostname
,
269 rq
->method
, rp
->status
);
270 fprintf(stdlog
, "proxy %s?", rq
->uri
);
272 while((s
= read(0, buffer
, rq
->size
>
273 sizeof(buffer
) ? sizeof(buffer
) : rq
->size
)) > 0) {
274 write(fd
, buffer
, s
);
277 if(stdlog
!= (FILE *)NULL
)
278 while(s
--) fputc(*b
++, stdlog
);
279 if(rq
->size
== 0) break;
281 if(stdlog
!= (FILE *)NULL
) {
282 fprintf(stdlog
, "\n");
286 while((s
= read(fd
, buffer
, sizeof(buffer
))) > 0) {