Annotation of libwww/Library/src/HTTP.c, revision 1.83
1.74 frystyk 1: /* HTTP.c
2: ** MULTITHREADED IMPLEMENTATION OF HTTP CLIENT
1.2 timbl 3: **
1.74 frystyk 4: ** (c) COPYRIGHT CERN 1994.
5: ** Please first read the full copyright statement in the file COPYRIGH.
6: **
7: ** This module implments the HTTP protocol as a state machine
1.55 frystyk 8: **
9: ** History:
1.59 frystyk 10: ** < May 24 94 ?? Unknown - but obviously written
1.56 frystyk 11: ** May 24 94 HF Made reentrent and cleaned up a bit. Implemented
12: ** Forward, redirection, error handling and referer field
1.67 duns 13: ** 8 Jul 94 FM Insulate free() from _free structure element.
1.71 frystyk 14: ** Jul 94 HFN Written on top of HTTP.c, Henrik Frystyk
1.55 frystyk 15: **
1.1 timbl 16: */
17:
1.78 frystyk 18: /* Library include files */
19: #include "tcp.h"
1.1 timbl 20: #include "HTUtils.h"
1.78 frystyk 21: #include "HTString.h"
1.71 frystyk 22: #include "HTParse.h"
1.1 timbl 23: #include "HTTCP.h"
24: #include "HTFormat.h"
1.2 timbl 25: #include "HTAlert.h"
26: #include "HTMIME.h"
1.21 luotonen 27: #include "HTAccess.h" /* HTRequest */
1.14 luotonen 28: #include "HTAABrow.h" /* Access Authorization */
1.20 timbl 29: #include "HTTee.h" /* Tee off a cache stream */
1.78 frystyk 30: #include "HTFWrite.h" /* Write to cache file */
1.80 frystyk 31: #include "HTWriter.h"
1.54 luotonen 32: #include "HTError.h"
1.55 frystyk 33: #include "HTChunk.h"
1.71 frystyk 34: #include "HTGuess.h"
35: #include "HTThread.h"
1.80 frystyk 36: #include "HTTPReq.h"
1.55 frystyk 37: #include "HTTP.h" /* Implements */
38:
39: /* Macros and other defines */
1.71 frystyk 40: #define PUTC(c) (*me->target->isa->put_character)(me->target, c)
41: #define PUTS(s) (*me->target->isa->put_string)(me->target, s)
42: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
43: #define FREE_TARGET (*me->target->isa->_free)(me->target)
1.74 frystyk 44: #define ABORT_TARGET (*me->target->isa->abort)(me->target, e)
1.2 timbl 45:
1.59 frystyk 46: /* Type definitions and global variables etc. local to this module */
47: /* This is the local definition of HTRequest->net_info */
48: typedef enum _HTTPState {
1.81 frystyk 49: HTTP_RETRY = -4,
1.71 frystyk 50: HTTP_ERROR = -3,
51: HTTP_NO_DATA = -2,
52: HTTP_GOT_DATA = -1,
53: HTTP_BEGIN = 0,
54: HTTP_NEED_CONNECTION,
55: HTTP_NEED_REQUEST,
56: HTTP_REDIRECTION,
57: HTTP_AA
1.59 frystyk 58: } HTTPState;
1.55 frystyk 59:
60: typedef struct _http_info {
1.78 frystyk 61: SOCKFD sockfd; /* Socket descripter */
1.71 frystyk 62: SockA sock_addr; /* SockA is defined in tcp.h */
1.68 frystyk 63: HTInputSocket * isoc; /* Input buffer */
1.80 frystyk 64: SocAction action; /* Result of the select call */
65: HTStream * target; /* Target stream */
1.71 frystyk 66: int addressCount; /* Attempts if multi-homed host */
67: time_t connecttime; /* Used on multihomed hosts */
68: struct _HTRequest * request; /* Link back to request structure */
1.68 frystyk 69:
1.81 frystyk 70: HTTPState state; /* Current State of the connection */
71: HTTPState next; /* Next state */
1.55 frystyk 72: } http_info;
73:
1.80 frystyk 74: #define MAX_STATUS_LEN 75 /* Max nb of chars to check StatusLine */
1.55 frystyk 75:
1.71 frystyk 76: struct _HTStream {
77: CONST HTStreamClass * isa;
78: HTStream * target;
79: HTRequest * request;
80: http_info * http;
81: HTSocketEOL state;
82: BOOL transparent;
1.81 frystyk 83: char * version; /* Should we save this? */
1.71 frystyk 84: int status;
1.81 frystyk 85: char * reason;
1.71 frystyk 86: char buffer[MAX_STATUS_LEN+1];
1.80 frystyk 87: int buflen;
1.71 frystyk 88: };
1.21 luotonen 89:
1.71 frystyk 90: /* ------------------------------------------------------------------------- */
91: /* Help Functions */
92: /* ------------------------------------------------------------------------- */
1.21 luotonen 93:
1.71 frystyk 94: /* HTTPCleanup
1.1 timbl 95: **
1.55 frystyk 96: ** This function closes the connection and frees memory.
1.1 timbl 97: **
1.55 frystyk 98: ** Returns 0 on OK, else -1
1.1 timbl 99: */
1.80 frystyk 100: PRIVATE int HTTPCleanup ARGS2(HTRequest *, req, BOOL, abort)
1.1 timbl 101: {
1.71 frystyk 102: http_info *http;
1.55 frystyk 103: int status = 0;
1.80 frystyk 104: if (!req || !req->net_info) {
1.78 frystyk 105: if (PROT_TRACE) fprintf(TDEST, "HTTPCleanup. Bad argument!\n");
1.55 frystyk 106: status = -1;
107: } else {
1.80 frystyk 108: http = (http_info *) req->net_info;
1.78 frystyk 109: if (http->sockfd != INVSOC) {
1.80 frystyk 110:
111: /* Free stream with data TO network */
112: if (!req->PostCallBack) {
113: if (abort)
114: (*req->input_stream->isa->abort)(req->input_stream, NULL);
115: else
116: (*req->input_stream->isa->_free)(req->input_stream);
117: }
118:
119: /* Free stream with data FROM network */
120: if (abort)
121: (*http->target->isa->abort)(http->target, NULL);
122: else
123: (*http->target->isa->_free)(http->target);
124:
1.78 frystyk 125: if (PROT_TRACE)
126: fprintf(TDEST,"HTTP........ Closing socket %d\n",http->sockfd);
1.59 frystyk 127: if ((status = NETCLOSE(http->sockfd)) < 0)
1.78 frystyk 128: HTErrorSysAdd(http->request, ERR_FATAL, socerrno, NO,
129: "NETCLOSE");
1.71 frystyk 130: HTThreadState(http->sockfd, THD_CLOSE);
1.80 frystyk 131: http->sockfd = INVSOC;
1.71 frystyk 132: HTThread_clear((HTNetInfo *) http);
133: }
134: if (http->isoc)
135: HTInputSocket_free(http->isoc);
1.80 frystyk 136: free(http);
137: req->net_info = NULL;
1.55 frystyk 138: }
139: return status;
140: }
141:
142:
1.71 frystyk 143: PRIVATE BOOL HTTPAuthentication ARGS1(HTRequest *, request)
144: {
145: HTAAScheme scheme;
146: HTList *valid_schemes = HTList_new();
147: HTAssocList **scheme_specifics = NULL;
1.76 frystyk 148: char *tmplate = NULL;
1.71 frystyk 149:
150: if (request->WWWAAScheme) {
151: if ((scheme = HTAAScheme_enum(request->WWWAAScheme)) != HTAA_UNKNOWN) {
152: HTList_addObject(valid_schemes, (void *) scheme);
153: if (!scheme_specifics) {
154: int i;
155: scheme_specifics = (HTAssocList**)
156: malloc(HTAA_MAX_SCHEMES * sizeof(HTAssocList*));
157: if (!scheme_specifics)
158: outofmem(__FILE__, "HTTPAuthentication");
159: for (i=0; i < HTAA_MAX_SCHEMES; i++)
160: scheme_specifics[i] = NULL;
161: }
162: scheme_specifics[scheme] = HTAA_parseArgList(request->WWWAARealm);
163: } else if (PROT_TRACE) {
164: HTErrorAdd(request, ERR_INFO, NO, HTERR_UNKNOWN_AA,
165: (void *) request->WWWAAScheme, 0, "HTTPAuthentication");
166: return NO;
167: }
168: }
169: if (request->WWWprotection) {
170: if (PROT_TRACE)
1.78 frystyk 171: fprintf(TDEST, "Protection template set to `%s'\n",
1.71 frystyk 172: request->WWWprotection);
1.76 frystyk 173: StrAllocCopy(tmplate, request->WWWprotection);
1.71 frystyk 174: }
175: request->valid_schemes = valid_schemes;
176: request->scheme_specifics = scheme_specifics;
1.76 frystyk 177: request->prot_template = tmplate;
1.71 frystyk 178: return YES;
179: }
180:
181:
182: /*
183: ** This is a big switch handling all HTTP return codes. It puts in any
184: ** appropiate error message and decides whether we should expect data
1.78 frystyk 185: ** or not.
1.55 frystyk 186: */
1.81 frystyk 187: PRIVATE void HTTPNextState ARGS1(HTStream *, me)
1.55 frystyk 188: {
1.71 frystyk 189: switch (me->status) {
190:
1.78 frystyk 191: case 0: /* 0.9 response */
192: case 200:
193: case 201:
194: case 202:
195: case 203:
196: case 205:
197: case 206:
1.81 frystyk 198: me->http->next = HTTP_GOT_DATA;
1.71 frystyk 199: break;
1.78 frystyk 200:
201: case 204: /* No Response */
1.81 frystyk 202: me->http->next = HTTP_NO_DATA;
1.71 frystyk 203: break;
1.78 frystyk 204:
1.71 frystyk 205: case 301: /* Moved */
206: case 302: /* Found */
1.81 frystyk 207: me->http->next = HTTP_REDIRECTION;
1.71 frystyk 208: break;
1.55 frystyk 209:
1.71 frystyk 210: case 303: /* Method */
211: HTAlert("This client doesn't support automatic redirection of type `Method'");
1.81 frystyk 212: me->http->next = HTTP_ERROR;
1.71 frystyk 213: break;
1.55 frystyk 214:
1.78 frystyk 215: case 400: /* Bad Request */
216: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_REQUEST,
1.81 frystyk 217: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
218: me->http->next = HTTP_ERROR;
1.71 frystyk 219: break;
1.70 howcome 220:
1.71 frystyk 221: case 401:
1.78 frystyk 222: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
1.81 frystyk 223: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
224: me->http->next = HTTP_AA;
1.71 frystyk 225: break;
226:
227: case 402: /* Payment required */
1.78 frystyk 228: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_PAYMENT_REQUIRED,
1.81 frystyk 229: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
230: me->http->next = HTTP_ERROR;
1.71 frystyk 231: break;
1.55 frystyk 232:
1.71 frystyk 233: case 403: /* Forbidden */
1.78 frystyk 234: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_FORBIDDEN,
1.81 frystyk 235: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
236: me->http->next = HTTP_ERROR;
1.71 frystyk 237: break;
1.55 frystyk 238:
1.71 frystyk 239: case 404: /* Not Found */
1.78 frystyk 240: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_FOUND,
1.81 frystyk 241: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
242: me->http->next = HTTP_ERROR;
1.71 frystyk 243: break;
244:
1.78 frystyk 245: case 405: /* Not Allowed */
246: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_ALLOWED,
1.81 frystyk 247: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
248: me->http->next = HTTP_ERROR;
1.78 frystyk 249: break;
250:
251: case 406: /* None Acceptable */
252: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NONE_ACCEPTABLE,
1.81 frystyk 253: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
254: me->http->next = HTTP_ERROR;
1.78 frystyk 255: break;
256:
257: case 407: /* Proxy Authentication Required */
258: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_PROXY,
1.81 frystyk 259: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
260: me->http->next = HTTP_ERROR;
1.78 frystyk 261: break;
262:
263: case 408: /* Request Timeout */
264: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_TIMEOUT,
1.81 frystyk 265: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
266: me->http->next = HTTP_ERROR;
1.78 frystyk 267: break;
268:
1.71 frystyk 269: case 500:
1.78 frystyk 270: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_INTERNAL,
1.81 frystyk 271: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
272: me->http->next = HTTP_ERROR;
1.78 frystyk 273: break;
274:
1.71 frystyk 275: case 501:
1.78 frystyk 276: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_IMPLEMENTED,
1.81 frystyk 277: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
278: me->http->next = HTTP_ERROR;
1.78 frystyk 279: break;
280:
281: case 502:
282: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_GATE,
1.81 frystyk 283: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
284: me->http->next = HTTP_ERROR;
1.78 frystyk 285: break;
286:
287: case 503:
288: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_DOWN,
1.81 frystyk 289: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
290:
291: /* If Retry-After header is found then return HT_RETRY else HT_ERROR */
292: if (me->request->retry_after)
293: me->http->next = HTTP_RETRY;
294: else
295: me->http->next = HTTP_ERROR;
1.78 frystyk 296: break;
297:
298: case 504:
299: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_GATE_TIMEOUT,
1.81 frystyk 300: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
301: me->http->next = HTTP_ERROR;
1.71 frystyk 302: break;
1.78 frystyk 303:
1.71 frystyk 304: default: /* bad number */
305: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_REPLY,
1.80 frystyk 306: (void *) me->buffer, me->buflen, "HTLoadHTTP");
1.81 frystyk 307: me->http->next = HTTP_ERROR;
1.71 frystyk 308: break;
1.55 frystyk 309: }
310: }
311:
1.71 frystyk 312: /* ------------------------------------------------------------------------- */
313: /* HTTP Status Line Stream */
314: /* ------------------------------------------------------------------------- */
1.55 frystyk 315:
1.71 frystyk 316: /*
1.80 frystyk 317: ** Analyse the stream we have read. If it is a HTTP 1.0 or higher
1.71 frystyk 318: ** then create a MIME-stream, else create a Guess stream to find out
319: ** what the 0.9 server is sending. We need to copy the buffer as we don't
320: ** know if we can modify the contents or not.
1.78 frystyk 321: **
322: ** Stream handling is a function of the status code returned from the
323: ** server:
324: ** 200: Use `output_stream' in HTRequest structure
1.80 frystyk 325: ** else: Use `error_stream' in HTRequest structure
326: **
327: ** Return: YES if buffer should be written out. NO otherwise
1.56 frystyk 328: */
1.80 frystyk 329: PRIVATE int stream_pipe ARGS1(HTStream *, me)
1.56 frystyk 330: {
1.71 frystyk 331: HTRequest *req = me->request;
1.80 frystyk 332: if (me->target) {
333: int status = PUTBLOCK(me->buffer, me->buflen);
334: if (status == HT_OK)
335: me->transparent = YES;
336: return status;
337: }
1.81 frystyk 338: if (strncasecomp(me->buffer, "http/", 5)) {
1.80 frystyk 339: int status;
340: HTErrorAdd(req, ERR_INFO, NO, HTERR_HTTP09,
341: (void *) me->buffer, me->buflen, "HTTPStatusStream");
342: me->target = HTGuess_new(req, NULL, WWW_UNKNOWN,
343: req->output_format, req->output_stream);
344: if ((status = PUTBLOCK(me->buffer, me->buflen)) == HT_OK)
345: me->transparent = YES;
346: return status;
347: } else {
1.81 frystyk 348: char *ptr = me->buffer+5; /* Skip the HTTP part */
349: me->version = HTNextField(&ptr);
350: me->status = atoi(HTNextField(&ptr));
351: me->reason = ptr;
352: if ((ptr = strchr(me->reason, '\r')) != NULL) /* Strip \r and \n */
353: *ptr = '0円';
354: else if ((ptr = strchr(me->reason, '\n')) != NULL)
355: *ptr = '0円';
356:
357: /* Set up the streams */
358: if (me->status==200) {
1.80 frystyk 359: HTStream *s;
360: me->target = HTStreamStack(WWW_MIME, req->output_format,
361: req->output_stream, req, NO);
362:
363: /* howcome: test for return value from HTCacheWriter 12/1/95 */
1.82 frystyk 364: if (req->method==METHOD_GET && HTCache_isEnabled() &&
1.80 frystyk 365: (s = HTCacheWriter(req, NULL, WWW_MIME, req->output_format,
366: req->output_stream))) {
367: me->target = HTTee(me->target, s);
368: }
1.81 frystyk 369: } else if (req->output_format == WWW_SOURCE) {
370: me->target = HTMIMEConvert(req, NULL, WWW_MIME, req->output_format,
371: req->output_stream);
1.71 frystyk 372: } else {
1.81 frystyk 373: me->target = HTMIMEConvert(req, NULL, WWW_MIME, req->error_format,
374: req->error_stream);
1.56 frystyk 375: }
1.80 frystyk 376: if (!me->target)
377: me->target = HTBlackHole(); /* What else */
1.56 frystyk 378: }
1.81 frystyk 379: HTTPNextState(me); /* Get next state */
1.80 frystyk 380: me->transparent = YES;
381: return HT_OK;
1.71 frystyk 382: }
1.56 frystyk 383:
1.80 frystyk 384: /*
385: ** Searches for HTTP header line until buffer fills up or a CRLF or LF
386: ** is found
387: */
388: PRIVATE int HTTPStatus_put_block ARGS3(HTStream *, me, CONST char*, b, int, l)
1.71 frystyk 389: {
1.80 frystyk 390: while (!me->transparent && l-- > 0) {
391: int status;
392: if (me->target) {
393: if ((status = stream_pipe(me)) != HT_OK)
394: return status;
395: } else {
396: *(me->buffer+me->buflen++) = *b;
397: if (me->state == EOL_FCR) {
398: if (*b == LF) { /* Line found */
399: if ((status = stream_pipe(me)) != HT_OK)
400: return status;
401: } else {
402: me->state = EOL_BEGIN;
403: }
404: } else if (*b == CR) {
405: me->state = EOL_FCR;
406: } else if (*b == LF) {
407: if ((status = stream_pipe(me)) != HT_OK)
408: return status;
1.71 frystyk 409: } else {
1.80 frystyk 410: if (me->buflen >= MAX_STATUS_LEN) {
411: if ((status = stream_pipe(me)) != HT_OK)
412: return status;
413: }
1.71 frystyk 414: }
1.80 frystyk 415: b++;
1.71 frystyk 416: }
1.56 frystyk 417: }
1.80 frystyk 418: if (l > 0)
419: return PUTBLOCK(b, l);
420: return HT_OK;
1.56 frystyk 421: }
422:
1.80 frystyk 423: PRIVATE int HTTPStatus_put_string ARGS2(HTStream *, me, CONST char*, s)
1.71 frystyk 424: {
1.80 frystyk 425: return HTTPStatus_put_block(me, s, (int) strlen(s));
1.71 frystyk 426: }
1.56 frystyk 427:
1.80 frystyk 428: PRIVATE int HTTPStatus_put_character ARGS2(HTStream *, me, char, c)
1.71 frystyk 429: {
1.80 frystyk 430: return HTTPStatus_put_block(me, &c, 1);
431: }
432:
433: PRIVATE int HTTPStatus_flush ARGS1(HTStream *, me)
434: {
435: return (*me->target->isa->flush)(me->target);
1.71 frystyk 436: }
437:
438: PRIVATE int HTTPStatus_free ARGS1(HTStream *, me)
439: {
440: if (me->target)
441: FREE_TARGET;
442: free(me);
1.80 frystyk 443: return HT_OK;
1.71 frystyk 444: }
445:
446: PRIVATE int HTTPStatus_abort ARGS2(HTStream *, me, HTError, e)
447: {
448: if (me->target)
1.74 frystyk 449: ABORT_TARGET;
1.71 frystyk 450: free(me);
1.74 frystyk 451: if (PROT_TRACE)
1.80 frystyk 452: fprintf(TDEST, "HTTPStatus.. ABORTING...\n");
453: return HT_ERROR;
1.71 frystyk 454: }
455:
456: /* HTTPStatus Stream
457: ** -----------------
458: */
459: PRIVATE CONST HTStreamClass HTTPStatusClass =
460: {
461: "HTTPStatus",
1.80 frystyk 462: HTTPStatus_flush,
1.71 frystyk 463: HTTPStatus_free,
464: HTTPStatus_abort,
465: HTTPStatus_put_character,
466: HTTPStatus_put_string,
467: HTTPStatus_put_block
468: };
469:
470: PUBLIC HTStream * HTTPStatus_new ARGS2(HTRequest *, request,
471: http_info *, http)
472: {
473: HTStream * me = (HTStream *) calloc(1, sizeof(HTStream));
474: if (!me) outofmem(__FILE__, "HTTPStatus_new");
475: me->isa = &HTTPStatusClass;
476: me->request = request;
477: me->http = http;
478: me->state = EOL_BEGIN;
479: return me;
480: }
481:
482: /* ------------------------------------------------------------------------- */
483:
484: /* Load Document from HTTP Server HTLoadHTTP
1.55 frystyk 485: ** ==============================
486: **
487: ** Given a hypertext address, this routine loads a document.
488: **
489: ** On entry,
490: ** request This is the request structure
491: ** On exit,
1.80 frystyk 492: ** returns HT_ERROR Error has occured or interrupted
1.71 frystyk 493: ** HT_WOULD_BLOCK if operation would have blocked
1.58 frystyk 494: ** HT_LOADED if return status 200 OK
495: ** HT_NO_DATA if return status 204 No Response
1.81 frystyk 496: ** HT_RETRY if return status 503 Service Unavail.
1.55 frystyk 497: */
498: PUBLIC int HTLoadHTTP ARGS1 (HTRequest *, request)
499: {
1.71 frystyk 500: int status = HT_ERROR;
501: char *url; /* Gets initialized on every entry */
1.55 frystyk 502: http_info *http; /* Specific protocol information */
503:
504: if (!request || !request->anchor) {
1.78 frystyk 505: if (PROT_TRACE) fprintf(TDEST, "HTLoadHTTP.. Bad argument\n");
1.71 frystyk 506: return HT_ERROR;
1.55 frystyk 507: }
508: url = HTAnchor_physical(request->anchor);
1.17 timbl 509:
1.71 frystyk 510: /* Only do the setup first time through. This is actually state HTTP_BEGIN
511: but it can't be in the state machine as we need the structure first */
512: if (!request->net_info) {
1.22 luotonen 513: /*
1.71 frystyk 514: ** Initiate a new http structure and bind to request structure
515: ** This is actually state HTTP_BEGIN, but it can't be in the state
516: ** machine as we need the structure first.
1.22 luotonen 517: */
1.80 frystyk 518: if (PROT_TRACE) fprintf(TDEST, "HTTP........ Looking for `%s\'\n",url);
1.71 frystyk 519: if ((http = (http_info *) calloc(1, sizeof(http_info))) == NULL)
520: outofmem(__FILE__, "HTLoadHTTP");
1.78 frystyk 521: http->sockfd = INVSOC; /* Invalid socket number */
1.71 frystyk 522: http->request = request;
523: http->state = HTTP_BEGIN;
524: request->net_info = (HTNetInfo *) http;
525: HTThread_new((HTNetInfo *) http);
1.80 frystyk 526: request->input_stream = HTTPRequest_new(request,request->input_stream);
1.71 frystyk 527: } else
528: http = (http_info *) request->net_info; /* Get existing copy */
529:
530: /* Now jump into the machine. We know the state from the previous run */
531: while (1) {
532: switch (http->state) {
533: case HTTP_BEGIN:
534: /*
535: ** Compose authorization information (this was moved here
536: ** from after the making of the connection so that the connection
537: ** wouldn't have to wait while prompting username and password
538: ** from the user). -- AL 13.10.93
539: */
540: HTAA_composeAuth(request);
541: if (PROT_TRACE) {
542: if (request->authorization)
1.78 frystyk 543: fprintf(TDEST, "HTTP........ Sending Authorization: %s\n",
1.71 frystyk 544: request->authorization);
545: else
1.78 frystyk 546: fprintf(TDEST,
1.71 frystyk 547: "HTTP........ Not sending authorization (yet)\n");
548: }
549: http->state = HTTP_NEED_CONNECTION;
550: break;
551:
552: case HTTP_NEED_CONNECTION: /* Now let's set up a connection */
553: status = HTDoConnect((HTNetInfo *) http, url, TCP_PORT,
554: NULL, NO);
1.80 frystyk 555: if (status == HT_OK) {
1.71 frystyk 556: if (PROT_TRACE)
1.78 frystyk 557: fprintf(TDEST, "HTTP........ Connected, socket %d\n",
1.71 frystyk 558: http->sockfd);
1.80 frystyk 559:
560: /* Set up read buffer, streams and concurrent read/write */
1.71 frystyk 561: http->isoc = HTInputSocket_new(http->sockfd);
1.80 frystyk 562: request->input_stream->target=HTWriter_new(http->sockfd, YES);
563: http->target = HTImProxy ?
564: request->output_stream : HTTPStatus_new(request, http);
565: HTThreadState(http->isoc->input_file_number, THD_SET_READ);
1.71 frystyk 566: http->state = HTTP_NEED_REQUEST;
567: } else if (status == HT_WOULD_BLOCK)
568: return status;
569: else
570: http->state = HTTP_ERROR; /* Error or interrupt */
571: break;
572:
1.80 frystyk 573: /* As we can do simultanous read and write this is now 1 state */
574: case HTTP_NEED_REQUEST:
575: if (http->action == SOC_WRITE) {
576:
577: /* Find the right way to call back */
578: if (request->CopyRequest) {
579: if (!HTAnchor_headerParsed(request->CopyRequest->anchor))
580: return HT_WOULD_BLOCK;
581: status = request->PostCallBack(request->CopyRequest,
582: request->input_stream);
583: } else if (request->PostCallBack) {
584: status = request->PostCallBack(request,
585: request->input_stream);
586: } else {
587: status = (*request->input_stream->isa->flush)
588: (request->input_stream);
589: }
1.71 frystyk 590: if (status == HT_WOULD_BLOCK)
1.80 frystyk 591: return HT_WOULD_BLOCK;
592: else if (status == HT_INTERRUPTED)
593: http->state = HTTP_ERROR;
1.71 frystyk 594: else
1.80 frystyk 595: http->action = SOC_READ;
596: } else if (http->action == SOC_READ) {
597: status = HTSocketRead(request, http->target);
598: if (status == HT_WOULD_BLOCK)
599: return HT_WOULD_BLOCK;
600: else if (status == HT_INTERRUPTED)
1.71 frystyk 601: http->state = HTTP_ERROR;
1.80 frystyk 602: else if (status == HT_LOADED) {
1.81 frystyk 603: http->state = http->next; /* Jump to next state */
1.80 frystyk 604: } else
605: http->state = HTTP_ERROR;
606: } else
1.78 frystyk 607: http->state = HTTP_ERROR;
1.71 frystyk 608: break;
1.80 frystyk 609:
610: case HTTP_REDIRECTION:
1.71 frystyk 611: if (request->redirect) {
612: HTAnchor *anchor;
613: if (status == 301) {
614: HTErrorAdd(request, ERR_INFO, NO, HTERR_MOVED,
615: (void *) request->redirect,
616: (int) strlen(request->redirect), "HTLoadHTTP");
617: } else if (status == 302) {
618: HTErrorAdd(request, ERR_INFO, NO, HTERR_FOUND,
619: (void *) request->redirect,
620: (int) strlen(request->redirect), "HTLoadHTTP");
1.55 frystyk 621: }
1.71 frystyk 622: anchor = HTAnchor_findAddress(request->redirect);
623: if (++request->redirections < HTMaxRedirections) {
1.80 frystyk 624: HTTPCleanup(request, NO);
1.71 frystyk 625: return HTLoadAnchorRecursive((HTAnchor *) anchor, request);
626: } else {
627: HTErrorAdd(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
1.58 frystyk 628: NULL, 0, "HTLoadHTTP");
1.71 frystyk 629: http->state = HTTP_ERROR;
1.58 frystyk 630: }
1.71 frystyk 631: } else {
632: HTErrorAdd(request, ERR_FATAL, NO, HTERR_BAD_REPLY,
633: NULL, 0, "HTLoadHTTP");
634: http->state = HTTP_ERROR;
635: }
636: break;
1.80 frystyk 637:
1.71 frystyk 638: case HTTP_AA:
1.80 frystyk 639: HTTPCleanup(request, NO); /* Close connection */
1.71 frystyk 640: if (HTTPAuthentication(request) == YES &&
641: HTAA_retryWithAuth(request) == YES) {
642: return HTLoadAnchor((HTAnchor *) request->anchor, request);
643: } else {
644: char *unescaped = NULL;
645: StrAllocCopy(unescaped, url);
646: HTUnEscape(unescaped);
647: HTErrorAdd(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
648: (void *) unescaped,
649: (int) strlen(unescaped), "HTLoadHTTP");
650: free(unescaped);
1.80 frystyk 651: return HT_ERROR;
1.71 frystyk 652: }
653: break;
1.80 frystyk 654:
1.71 frystyk 655: case HTTP_GOT_DATA:
1.80 frystyk 656: HTTPCleanup(request, NO);
1.71 frystyk 657: return HT_LOADED;
658: break;
659:
660: case HTTP_NO_DATA:
1.80 frystyk 661: HTTPCleanup(request, NO);
1.71 frystyk 662: return HT_NO_DATA;
663: break;
1.80 frystyk 664:
1.81 frystyk 665: case HTTP_RETRY:
666: HTTPCleanup(request, YES);
667: return HT_RETRY;
668: break;
669:
1.71 frystyk 670: case HTTP_ERROR:
1.80 frystyk 671: HTTPCleanup(request, YES);
1.71 frystyk 672: return HT_ERROR;
673: break;
674: }
675: } /* End of while(1) */
676: }
677:
678: /* Protocol descriptor */
679:
680: GLOBALDEF PUBLIC HTProtocol HTTP = {
681: "http", SOC_NON_BLOCK, HTLoadHTTP, NULL, NULL
682: };
1.21 luotonen 683:
Webmaster