1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/*
** ____ _
** ___| _ \ ___ _ __| |
** / _ \ |_) / _ \ '__| |
** | __/ __/ __/ | | |
** \___|_| \___|_| |_|
**
** ePerl -- Embedded Perl 5 Language
**
** ePerl interprets an ASCII file bristled with Perl 5 program statements
** by evaluating the Perl 5 code while passing through the plain ASCII
** data. It can operate both as a standard Unix filter for general file
** generation tasks and as a powerful Webserver scripting language for
** dynamic HTML page programming.
**
** ======================================================================
**
** Copyright (c) 1996,1997,1998,1999 Ralf S. Engelschall <rse@engelschall.com>
**
** This program is free software; it may be redistributed and/or modified
** only under the terms of either the Artistic License or the GNU General
** Public License, which may be found in the ePerl source distribution.
** Look at the files ARTISTIC and COPYING or run ``eperl -l'' to receive
** a built-in copy of both license files.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
** Artistic License or the GNU General Public License for more details.
**
** ======================================================================
**
** eperl_http.c -- ePerl HTTP stuff
*/
#include "eperl_config.h"
#include "eperl_global.h"
#include "eperl_proto.h"
#define _EPERL_VERSION_C_AS_HEADER_
#include "eperl_version.c"
#undef _EPERL_VERSION_C_AS_HEADER_
/*
**
** print a standard HTTP reponse of header lines
**
*/
char *HTTP_PrintResponseHeaders(char *cpBuf)
{
char *cp;
if ((strncmp(cpBuf, "HTTP/1.0 ", 9) == 0
|| (strncmp(cpBuf, "HTTP/1.1 ", 9) == 0))
&& (cpBuf[9] >= '1' && cpBuf[9] <= '5')
&& (cpBuf[10] >= '0' && cpBuf[10] <= '9')
&& (cpBuf[11] >= '0' && cpBuf[11] <= '9')
&& (cpBuf[12] == ' ')
&& ((cp = strchr(cpBuf + 12, '\n')) != NULL)) {
/* found HTTP status code */
if (*(cp-1) == '\r') {
*(cp-1) = '\0';
}
*cp++ = '\0';
printf("%s\r\n", cpBuf);
cpBuf = cp;
} else {
/* no HTTP status code */
if ((cp = getenv("SERVER_PROTOCOL")) == NULL)
cp = "HTTP/1.0";
printf("%s 200 OK\r\n", cp);
}
if (!HTTP_HeaderLineExists(cpBuf, "Server")) {
if ((cp = getenv("SERVER_SOFTWARE")) == NULL)
cp = "unknown-server/0.0";
printf("Server: %s %s Perl/%s\r\n", cp, eperl_version.v_web, AC_perl_vers);
}
if (!HTTP_HeaderLineExists(cpBuf, "Date"))
printf("Date: %s\r\n", WebTime());
if (!HTTP_HeaderLineExists(cpBuf, "Connection"))
printf("Connection: close\r\n");
return cpBuf;
}
/*
**
** strip standard HTTP reponse header lines
**
*/
void HTTP_StripResponseHeaders(char **cpBuf, int *nBuf)
{
return;
}
/*
**
** check if the line is a valid HTTP header line
**
*/
int HTTP_IsHeaderLine(char *cp1, char *cp2)
{
char *cp3;
char *cp4;
char ca[1024];
while (cp1 < cp2 && (*cp1 == '\n' || *cp1 == '\r'))
cp1++;
while (cp2 > cp1 && (*(cp2-1) == '\n' || *(cp2-1) == '\r'))
cp2--;
strncpy(ca, cp1, cp2-cp1);
ca[cp2-cp1] = NUL;
if ((cp3 = strchr(ca, ':')) == NULL)
return 0;
for (cp4 = ca; cp4 < cp3; cp4++) {
if (! ((*cp4 >= 'A' && *cp4 <= 'Z') ||
(*cp4 >= 'a' && *cp4 <= 'z') ||
(*cp4 >= '0' && *cp4 <= '9') ||
(*cp4 == '-' || *cp4 == '_') ))
return 0;
}
return 1;
}
/*
**
** check if there is a valid HTTP header
**
*/
int HTTP_HeadersExists(char *cpBuf)
{
char *cp1;
char *cp2;
char *cp2a;
char *cp3;
cp2 = NULL;
if ((cp2a = strstr(cpBuf, "\n\n")) != NULL)
cp2 = cp2a;
if ((cp2a = strstr(cpBuf, "\r\n\r\n")) != NULL && (cp2 == NULL || cp2a < cp2))
cp2 = cp2a;
if (cp2 != NULL) {
for (cp1 = cpBuf; cp1 < cp2-1; ) {
cp3 = strchr(cp1, '\n');
if (!HTTP_IsHeaderLine(cp1, cp3))
return 0;
cp1 = cp3+1;
}
return 1;
}
return 0;
}
/*
**
** check if there a particular HTTP headerline exists
**
*/
int HTTP_HeaderLineExists(char *cpBuf, char *name)
{
char *cp1;
char *cp2;
char *cp2a;
char *cp3;
int n;
n = strlen(name);
cp2 = NULL;
if ((cp2a = strstr(cpBuf, "\n\n")) != NULL)
cp2 = cp2a;
if ((cp2a = strstr(cpBuf, "\r\n\r\n")) != NULL && (cp2 == NULL || cp2a < cp2))
cp2 = cp2a;
if (cp2 != NULL) {
for (cp1 = cpBuf; cp1 < cp2-1; ) {
cp3 = strchr(cp1, '\n');
if (HTTP_IsHeaderLine(cp1, cp3) && cp3-cp1 > n+1)
if (strncasecmp(cp1, name, n) == 0)
return 1;
cp1 = cp3+1;
}
return 0;
}
return 0;
}
/*
**
** Give back acceptable HTTP time format string
**
*/
char *WebTime(void)
{
time_t t;
struct tm *tm;
char *cp;
t = time(&t);
tm = localtime(&t);
cp = ctime(&t);
cp[strlen(cp)-1] = NUL;
return cp;
}
/*
** extracts the host name from an url
*/
static char *HTTP_HostOfURL(char *url)
{
static char host[1024];
char *cps;
char *cpe;
cps = strstr(url, "//");
cps += 2;
for (cpe = cps; *cpe != '/' && *cpe != ':' && *cpe != NUL; cpe++)
;
strncpy(host, cps, cpe-cps);
host[cpe-cps] = NUL;
return host;
}
/*
** extracts the port from an url
*/
static char *HTTP_PortOfURL(char *url)
{
static char port[128];
char *cps;
char *cpe;
cps = strstr(url, "//");
cps += 2;
for ( ; *cps != '/' && *cps != ':' && *cps != NUL; cps++)
;
if (*cps == ':') {
cps++;
for (cpe = cps; *cpe != '/' && *cpe != NUL; cpe++)
;
strncpy(port, cps, cpe-cps);
port[cpe-cps] = NUL;
}
else
strcpy(port, "80");
return port;
}
/*
** extracts a file name from a url
*/
static char *HTTP_FileOfURL(char *url)
{
static char file[2048];
char *cps;
cps = strstr(url, "//");
cps = strstr(cps+2, "/");
if (cps == NUL)
strcpy(file, "/");
else
strncpy(file, cps, sizeof(file));
file[sizeof(file)-1] = NUL;
return file;
}
/*
** open an URL as a file descriptor
*/
FILE *HTTP_openURLasFP(char *url)
{
struct hostent *he;
struct sockaddr_in sar;
struct protoent *pe;
char *cmd;
char buf[1024];
char newurl[8192];
char *host;
char *port;
char *file;
FILE *fp;
char *cp;
char *cp2;
int s;
/* parse URL */
host = HTTP_HostOfURL(url);
port = HTTP_PortOfURL(url);
file = HTTP_FileOfURL(url);
/* get the host name */
if ((he = gethostbyname(host)) == NULL)
return NULL;
/* get TCP protocol information */
if ((pe = getprotobyname("tcp")) == NULL)
return NULL;
/* open the socket */
if ((s = socket(AF_INET, SOCK_STREAM, pe->p_proto)) == -1)
return NULL;
/* fill in the socket information */
sar.sin_family = AF_INET;
sar.sin_addr.s_addr = *((u_long *)(he->h_addr_list[0]));
sar.sin_port = htons(atoi(port));
/* actually connect */
if (connect(s, (struct sockaddr *)&sar, sizeof(sar)) == -1)
return NULL;
/* form the HTTP/1.0 request */
cmd = malloc(64 + strlen(file) + strlen(host) +
strlen(port) + strlen(eperl_version.v_web));
if (cmd == NULL)
return NULL;
/* cmd has enough space */
sprintf(cmd, "GET %s HTTP/1.0\r\n", file);
sprintf(cmd+strlen(cmd), "Host: %s:%s\r\n", host, port);
sprintf(cmd+strlen(cmd), "User-Agent: %s\r\n", eperl_version.v_web);
sprintf(cmd+strlen(cmd), "\r\n");
/* send the request */
write(s, cmd, strlen(cmd));
free(cmd);
/* convert the file descriptor to a FILE pointer */
fp = fdopen(s, "r");
/* read the HTTP response line and check for 200 OK response */
if (fgets(buf, sizeof(buf), fp) == NULL)
return NULL;
if (strncmp(buf, "HTTP/1.", 7) != 0)
return NULL;
if (buf[7] != '0' && buf[7] != '1')
return NULL;
for (cp = buf+8; *cp == ' ' || *cp == '\t'; cp++)
;
if (strncmp(cp, "200", 3 /* OK */) != 0) {
if (strncmp(cp, "301", 3 /* MOVED PERMANENTLY */) != 0 ||
strncmp(cp, "302", 3 /* MOVED TEMPORARILY */) != 0 ) {
/* we try to determine the new URL from
the HTTP header 'Location' and restart from
the beginning if an URL is found */
newurl[0] = NUL;
while (fgets(buf, sizeof(buf), fp) != NULL) {
if ((*buf == '\n' && *(buf+1) == NUL) ||
(*buf == '\n' && *(buf+1) == '\r' && *(buf+2) == NUL) ||
(*buf == '\r' && *(buf+1) == '\n' && *(buf+2) == NUL))
break;
if (strncasecmp(buf, "Location:", 9) == 0) {
for (cp = buf+9; *cp == ' ' || *cp == '\t'; cp++)
;
for (cp2 = cp; *cp2 != ' ' && *cp2 != '\t' && *cp2 != '\n' && *cp2 != NUL; cp2++)
;
*cp2 = NUL;
strncpy(newurl, cp, sizeof(newurl));
newurl[sizeof(newurl)-1] = NUL;
break;
}
}
if (newurl[0] != NUL)
return HTTP_openURLasFP(newurl);
else
return NULL;
}
return NULL;
}
/* now read until a blank line, i.e. skip HTTP headers */
while (fgets(buf, sizeof(buf), fp) != NULL) {
if ((*buf == '\n' && *(buf+1) == NUL) ||
(*buf == '\n' && *(buf+1) == '\r' && *(buf+2) == NUL) ||
(*buf == '\r' && *(buf+1) == '\n' && *(buf+2) == NUL))
break;
}
/* return the (still open) FILE pointer */
return fp;
}
/*EOF*/
|