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