Annotation of libwww/Library/src/HTTP.c, revision 1.94
1.74 frystyk 1: /* HTTP.c
2: ** MULTITHREADED IMPLEMENTATION OF HTTP CLIENT
1.2 timbl 3: **
1.84 frystyk 4: ** (c) COPYRIGHT MIT 1995.
1.74 frystyk 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.85 frystyk 25: #include "HTSocket.h"
1.94 ! frystyk 26: #include "HTReqMan.h"
1.2 timbl 27: #include "HTAlert.h"
28: #include "HTMIME.h"
1.14 luotonen 29: #include "HTAABrow.h" /* Access Authorization */
1.20 timbl 30: #include "HTTee.h" /* Tee off a cache stream */
1.78 frystyk 31: #include "HTFWrite.h" /* Write to cache file */
1.90 frystyk 32: #include "HTCache.h"
1.80 frystyk 33: #include "HTWriter.h"
1.54 luotonen 34: #include "HTError.h"
1.94 ! frystyk 35: #include "HTAccess.h"
1.55 frystyk 36: #include "HTChunk.h"
1.71 frystyk 37: #include "HTGuess.h"
1.94 ! frystyk 38: #include "HTNet.h"
1.80 frystyk 39: #include "HTTPReq.h"
1.55 frystyk 40: #include "HTTP.h" /* Implements */
41:
42: /* Macros and other defines */
1.94 ! frystyk 43: #ifndef HTTP_PORT
! 44: #define HTTP_PORT 80 /* Allocated to http by Jon Postel/ISI 24-Jan-92 */
! 45: #endif
! 46:
1.71 frystyk 47: #define PUTC(c) (*me->target->isa->put_character)(me->target, c)
48: #define PUTS(s) (*me->target->isa->put_string)(me->target, s)
49: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
50: #define FREE_TARGET (*me->target->isa->_free)(me->target)
1.74 frystyk 51: #define ABORT_TARGET (*me->target->isa->abort)(me->target, e)
1.2 timbl 52:
1.59 frystyk 53: /* Type definitions and global variables etc. local to this module */
1.94 ! frystyk 54:
! 55: /* Final states have negative value */
1.59 frystyk 56: typedef enum _HTTPState {
1.81 frystyk 57: HTTP_RETRY = -4,
1.71 frystyk 58: HTTP_ERROR = -3,
59: HTTP_NO_DATA = -2,
60: HTTP_GOT_DATA = -1,
61: HTTP_BEGIN = 0,
62: HTTP_NEED_CONNECTION,
63: HTTP_NEED_REQUEST,
64: HTTP_REDIRECTION,
1.91 frystyk 65: HTTP_NOT_MODIFIED,
66: HTTP_EXPIRED,
1.71 frystyk 67: HTTP_AA
1.59 frystyk 68: } HTTPState;
1.55 frystyk 69:
1.94 ! frystyk 70: /* This is the context structure for the this module */
1.55 frystyk 71: typedef struct _http_info {
1.81 frystyk 72: HTTPState state; /* Current State of the connection */
73: HTTPState next; /* Next state */
1.55 frystyk 74: } http_info;
75:
1.88 frystyk 76: #define MAX_STATUS_LEN 100 /* Max nb of chars to check StatusLine */
1.55 frystyk 77:
1.71 frystyk 78: struct _HTStream {
79: CONST HTStreamClass * isa;
80: HTStream * target;
81: HTRequest * request;
82: http_info * http;
83: HTSocketEOL state;
84: BOOL transparent;
1.81 frystyk 85: char * version; /* Should we save this? */
1.71 frystyk 86: int status;
1.81 frystyk 87: char * reason;
1.71 frystyk 88: char buffer[MAX_STATUS_LEN+1];
1.80 frystyk 89: int buflen;
1.71 frystyk 90: };
1.21 luotonen 91:
1.71 frystyk 92: /* ------------------------------------------------------------------------- */
93: /* Help Functions */
94: /* ------------------------------------------------------------------------- */
1.21 luotonen 95:
1.94 ! frystyk 96: /* HTTPCleanup
! 97: ** -----------
1.55 frystyk 98: ** This function closes the connection and frees memory.
1.94 ! frystyk 99: ** Returns YES on OK, else NO
1.1 timbl 100: */
1.94 ! frystyk 101: PRIVATE int HTTPCleanup (HTRequest *req, int status)
1.1 timbl 102: {
1.94 ! frystyk 103: HTNet *net = req->net;
! 104: http_info *http = (http_info *) net->context;
1.80 frystyk 105:
1.94 ! frystyk 106: /* Free stream with data TO network */
! 107: if (!HTRequest_isDestination(req) && req->input_stream) {
! 108: if (status == HT_INTERRUPTED)
! 109: (*req->input_stream->isa->abort)(req->input_stream, NULL);
! 110: else
! 111: (*req->input_stream->isa->_free)(req->input_stream);
! 112: }
1.88 frystyk 113:
1.94 ! frystyk 114: /* Remove the request object and our own context structure for http */
! 115: HTNet_delete(net, status);
! 116: FREE(http);
! 117: return YES;
1.55 frystyk 118: }
119:
120:
1.94 ! frystyk 121: PRIVATE BOOL HTTPAuthentication (HTRequest * request)
1.71 frystyk 122: {
123: HTAAScheme scheme;
124: HTList *valid_schemes = HTList_new();
125: HTAssocList **scheme_specifics = NULL;
1.76 frystyk 126: char *tmplate = NULL;
1.71 frystyk 127:
128: if (request->WWWAAScheme) {
129: if ((scheme = HTAAScheme_enum(request->WWWAAScheme)) != HTAA_UNKNOWN) {
130: HTList_addObject(valid_schemes, (void *) scheme);
131: if (!scheme_specifics) {
132: int i;
133: scheme_specifics = (HTAssocList**)
134: malloc(HTAA_MAX_SCHEMES * sizeof(HTAssocList*));
135: if (!scheme_specifics)
136: outofmem(__FILE__, "HTTPAuthentication");
137: for (i=0; i < HTAA_MAX_SCHEMES; i++)
138: scheme_specifics[i] = NULL;
139: }
140: scheme_specifics[scheme] = HTAA_parseArgList(request->WWWAARealm);
141: } else if (PROT_TRACE) {
142: HTErrorAdd(request, ERR_INFO, NO, HTERR_UNKNOWN_AA,
143: (void *) request->WWWAAScheme, 0, "HTTPAuthentication");
144: return NO;
145: }
146: }
147: if (request->WWWprotection) {
148: if (PROT_TRACE)
1.78 frystyk 149: fprintf(TDEST, "Protection template set to `%s'\n",
1.71 frystyk 150: request->WWWprotection);
1.76 frystyk 151: StrAllocCopy(tmplate, request->WWWprotection);
1.71 frystyk 152: }
153: request->valid_schemes = valid_schemes;
154: request->scheme_specifics = scheme_specifics;
1.76 frystyk 155: request->prot_template = tmplate;
1.71 frystyk 156: return YES;
157: }
158:
159:
160: /*
161: ** This is a big switch handling all HTTP return codes. It puts in any
162: ** appropiate error message and decides whether we should expect data
1.78 frystyk 163: ** or not.
1.55 frystyk 164: */
1.81 frystyk 165: PRIVATE void HTTPNextState ARGS1(HTStream *, me)
1.55 frystyk 166: {
1.71 frystyk 167: switch (me->status) {
168:
1.78 frystyk 169: case 0: /* 0.9 response */
170: case 200:
171: case 201:
172: case 202:
173: case 203:
1.81 frystyk 174: me->http->next = HTTP_GOT_DATA;
1.71 frystyk 175: break;
1.78 frystyk 176:
177: case 204: /* No Response */
1.81 frystyk 178: me->http->next = HTTP_NO_DATA;
1.71 frystyk 179: break;
1.78 frystyk 180:
1.71 frystyk 181: case 301: /* Moved */
182: case 302: /* Found */
1.81 frystyk 183: me->http->next = HTTP_REDIRECTION;
1.71 frystyk 184: break;
1.55 frystyk 185:
1.71 frystyk 186: case 303: /* Method */
1.92 frystyk 187: HTAlert(me->request,
188: "This client doesn't support automatic redirection of type `Method'");
1.81 frystyk 189: me->http->next = HTTP_ERROR;
1.71 frystyk 190: break;
1.91 frystyk 191:
192: case 304: /* Not Modified */
193: me->http->next = HTTP_NOT_MODIFIED;
194: break;
1.55 frystyk 195:
1.78 frystyk 196: case 400: /* Bad Request */
197: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_REQUEST,
1.81 frystyk 198: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
199: me->http->next = HTTP_ERROR;
1.71 frystyk 200: break;
1.70 howcome 201:
1.71 frystyk 202: case 401:
1.78 frystyk 203: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
1.81 frystyk 204: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
205: me->http->next = HTTP_AA;
1.71 frystyk 206: break;
207:
208: case 402: /* Payment required */
1.78 frystyk 209: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_PAYMENT_REQUIRED,
1.81 frystyk 210: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
211: me->http->next = HTTP_ERROR;
1.71 frystyk 212: break;
1.55 frystyk 213:
1.71 frystyk 214: case 403: /* Forbidden */
1.78 frystyk 215: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_FORBIDDEN,
1.81 frystyk 216: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
217: me->http->next = HTTP_ERROR;
1.71 frystyk 218: break;
1.55 frystyk 219:
1.71 frystyk 220: case 404: /* Not Found */
1.78 frystyk 221: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_FOUND,
1.81 frystyk 222: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
223: me->http->next = HTTP_ERROR;
1.71 frystyk 224: break;
225:
1.78 frystyk 226: case 405: /* Not Allowed */
227: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_ALLOWED,
1.81 frystyk 228: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
229: me->http->next = HTTP_ERROR;
1.78 frystyk 230: break;
231:
232: case 406: /* None Acceptable */
233: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NONE_ACCEPTABLE,
1.81 frystyk 234: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
235: me->http->next = HTTP_ERROR;
1.78 frystyk 236: break;
237:
238: case 407: /* Proxy Authentication Required */
239: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_PROXY,
1.81 frystyk 240: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
241: me->http->next = HTTP_ERROR;
1.78 frystyk 242: break;
243:
244: case 408: /* Request Timeout */
245: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_TIMEOUT,
1.81 frystyk 246: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
247: me->http->next = HTTP_ERROR;
1.78 frystyk 248: break;
249:
1.71 frystyk 250: case 500:
1.78 frystyk 251: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_INTERNAL,
1.81 frystyk 252: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
253: me->http->next = HTTP_ERROR;
1.78 frystyk 254: break;
255:
1.71 frystyk 256: case 501:
1.78 frystyk 257: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_NOT_IMPLEMENTED,
1.81 frystyk 258: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
259: me->http->next = HTTP_ERROR;
1.78 frystyk 260: break;
261:
262: case 502:
263: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_GATE,
1.81 frystyk 264: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
265: me->http->next = HTTP_ERROR;
1.78 frystyk 266: break;
267:
268: case 503:
269: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_DOWN,
1.81 frystyk 270: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
271:
272: /* If Retry-After header is found then return HT_RETRY else HT_ERROR */
273: if (me->request->retry_after)
274: me->http->next = HTTP_RETRY;
275: else
276: me->http->next = HTTP_ERROR;
1.78 frystyk 277: break;
278:
279: case 504:
280: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_GATE_TIMEOUT,
1.81 frystyk 281: me->reason, (int) strlen(me->reason), "HTLoadHTTP");
282: me->http->next = HTTP_ERROR;
1.71 frystyk 283: break;
1.78 frystyk 284:
1.71 frystyk 285: default: /* bad number */
286: HTErrorAdd(me->request, ERR_FATAL, NO, HTERR_BAD_REPLY,
1.80 frystyk 287: (void *) me->buffer, me->buflen, "HTLoadHTTP");
1.81 frystyk 288: me->http->next = HTTP_ERROR;
1.71 frystyk 289: break;
1.55 frystyk 290: }
291: }
292:
1.71 frystyk 293: /* ------------------------------------------------------------------------- */
294: /* HTTP Status Line Stream */
295: /* ------------------------------------------------------------------------- */
1.55 frystyk 296:
1.71 frystyk 297: /*
1.80 frystyk 298: ** Analyse the stream we have read. If it is a HTTP 1.0 or higher
1.71 frystyk 299: ** then create a MIME-stream, else create a Guess stream to find out
300: ** what the 0.9 server is sending. We need to copy the buffer as we don't
301: ** know if we can modify the contents or not.
1.78 frystyk 302: **
303: ** Stream handling is a function of the status code returned from the
304: ** server:
305: ** 200: Use `output_stream' in HTRequest structure
1.94 ! frystyk 306: ** else: Use `debug_stream' in HTRequest structure
1.80 frystyk 307: **
308: ** Return: YES if buffer should be written out. NO otherwise
1.56 frystyk 309: */
1.80 frystyk 310: PRIVATE int stream_pipe ARGS1(HTStream *, me)
1.56 frystyk 311: {
1.71 frystyk 312: HTRequest *req = me->request;
1.80 frystyk 313: if (me->target) {
314: int status = PUTBLOCK(me->buffer, me->buflen);
315: if (status == HT_OK)
316: me->transparent = YES;
317: return status;
1.88 frystyk 318: } else if (HTRequest_isSource(req) && !req->output_stream) {
319: /*
320: ** We need to wait for the destinations to get ready
321: */
322: return HT_WOULD_BLOCK;
1.80 frystyk 323: }
1.88 frystyk 324:
1.81 frystyk 325: if (strncasecomp(me->buffer, "http/", 5)) {
1.80 frystyk 326: int status;
327: HTErrorAdd(req, ERR_INFO, NO, HTERR_HTTP09,
328: (void *) me->buffer, me->buflen, "HTTPStatusStream");
329: me->target = HTGuess_new(req, NULL, WWW_UNKNOWN,
330: req->output_format, req->output_stream);
1.88 frystyk 331: me->http->next = HTTP_GOT_DATA;
1.80 frystyk 332: if ((status = PUTBLOCK(me->buffer, me->buflen)) == HT_OK)
333: me->transparent = YES;
334: return status;
335: } else {
1.81 frystyk 336: char *ptr = me->buffer+5; /* Skip the HTTP part */
337: me->version = HTNextField(&ptr);
338: me->status = atoi(HTNextField(&ptr));
339: me->reason = ptr;
340: if ((ptr = strchr(me->reason, '\r')) != NULL) /* Strip \r and \n */
341: *ptr = '0円';
342: else if ((ptr = strchr(me->reason, '\n')) != NULL)
343: *ptr = '0円';
344:
345: /* Set up the streams */
346: if (me->status==200) {
1.80 frystyk 347: HTStream *s;
1.85 frystyk 348: if (req->output_format == WWW_SOURCE) {
349: me->target = HTMIMEConvert(req, NULL, WWW_MIME,
350: req->output_format,
351: req->output_stream);
352: } else {
353: me->target = HTStreamStack(WWW_MIME, req->output_format,
354: req->output_stream, req, NO);
1.80 frystyk 355:
1.85 frystyk 356: /* howcome: test for return value from HTCacheWriter 12/1/95 */
357: if (req->method==METHOD_GET && HTCache_isEnabled() &&
358: (s = HTCacheWriter(req, NULL, WWW_MIME, req->output_format,
359: req->output_stream))) {
360: me->target = HTTee(me->target, s);
361: }
1.89 frystyk 362: }
1.94 ! frystyk 363: } else if (req->debug_stream) {
! 364: me->target = HTStreamStack(WWW_MIME, req->debug_format,
! 365: req->debug_stream, req, NO);
1.71 frystyk 366: } else {
1.94 ! frystyk 367: me->target = HTMIMEConvert(req, NULL, WWW_MIME, WWW_PRESENT,
! 368: HTBlackHole());
1.56 frystyk 369: }
1.80 frystyk 370: if (!me->target)
371: me->target = HTBlackHole(); /* What else */
1.56 frystyk 372: }
1.81 frystyk 373: HTTPNextState(me); /* Get next state */
1.80 frystyk 374: me->transparent = YES;
375: return HT_OK;
1.71 frystyk 376: }
1.56 frystyk 377:
1.80 frystyk 378: /*
379: ** Searches for HTTP header line until buffer fills up or a CRLF or LF
380: ** is found
381: */
382: PRIVATE int HTTPStatus_put_block ARGS3(HTStream *, me, CONST char*, b, int, l)
1.71 frystyk 383: {
1.80 frystyk 384: while (!me->transparent && l-- > 0) {
385: int status;
386: if (me->target) {
387: if ((status = stream_pipe(me)) != HT_OK)
388: return status;
389: } else {
390: *(me->buffer+me->buflen++) = *b;
391: if (me->state == EOL_FCR) {
392: if (*b == LF) { /* Line found */
393: if ((status = stream_pipe(me)) != HT_OK)
394: return status;
395: } else {
396: me->state = EOL_BEGIN;
397: }
398: } else if (*b == CR) {
399: me->state = EOL_FCR;
400: } else if (*b == LF) {
401: if ((status = stream_pipe(me)) != HT_OK)
402: return status;
1.71 frystyk 403: } else {
1.80 frystyk 404: if (me->buflen >= MAX_STATUS_LEN) {
405: if ((status = stream_pipe(me)) != HT_OK)
406: return status;
407: }
1.71 frystyk 408: }
1.80 frystyk 409: b++;
1.71 frystyk 410: }
1.56 frystyk 411: }
1.88 frystyk 412: if (me->target) { /* Is the stream set up? */
413: if (l > 0) /* Anything left? */
414: return PUTBLOCK(b, l);
415: return HT_OK;
416: }
417: return HT_WOULD_BLOCK;
1.56 frystyk 418: }
419:
1.80 frystyk 420: PRIVATE int HTTPStatus_put_string ARGS2(HTStream *, me, CONST char*, s)
1.71 frystyk 421: {
1.80 frystyk 422: return HTTPStatus_put_block(me, s, (int) strlen(s));
1.71 frystyk 423: }
1.56 frystyk 424:
1.80 frystyk 425: PRIVATE int HTTPStatus_put_character ARGS2(HTStream *, me, char, c)
1.71 frystyk 426: {
1.80 frystyk 427: return HTTPStatus_put_block(me, &c, 1);
428: }
429:
430: PRIVATE int HTTPStatus_flush ARGS1(HTStream *, me)
431: {
432: return (*me->target->isa->flush)(me->target);
1.71 frystyk 433: }
434:
435: PRIVATE int HTTPStatus_free ARGS1(HTStream *, me)
436: {
1.87 frystyk 437: int status = HT_OK;
438: if (me->target) {
439: if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
440: return HT_WOULD_BLOCK;
441: }
1.71 frystyk 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:
1.94 ! frystyk 470: PRIVATE HTStream * HTTPStatus_new ARGS2(HTRequest *, request,
! 471: http_info *, http)
1.71 frystyk 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
1.94 ! frystyk 491: ** returns HT_ERROR Error has occured in call back
! 492: ** HT_OK Call back was OK
1.55 frystyk 493: */
1.94 ! frystyk 494: PUBLIC int HTLoadHTTP (SOCKET soc, HTRequest * request, SockOps ops)
1.55 frystyk 495: {
1.71 frystyk 496: int status = HT_ERROR;
1.94 ! frystyk 497: HTNet *net = request->net; /* Generic protocol information */
1.55 frystyk 498: http_info *http; /* Specific protocol information */
1.17 timbl 499:
1.94 ! frystyk 500: /*
! 501: ** Initiate a new http structure and bind to request structure
! 502: ** This is actually state HTTP_BEGIN, but it can't be in the state
! 503: ** machine as we need the structure first.
! 504: */
! 505: if (ops == FD_NONE) {
! 506: if (PROT_TRACE) fprintf(TDEST, "HTTP........ Looking for `%s\'\n",
! 507: HTAnchor_physical(request->anchor));
1.71 frystyk 508: if ((http = (http_info *) calloc(1, sizeof(http_info))) == NULL)
509: outofmem(__FILE__, "HTLoadHTTP");
510: http->state = HTTP_BEGIN;
1.85 frystyk 511: http->next = HTTP_ERROR;
1.94 ! frystyk 512: net->context = http;
! 513: } if (ops == FD_CLOSE) { /* Interrupted */
! 514: if(HTRequest_isPostWeb(request)&&!HTRequest_isMainDestination(request))
! 515: HTTPCleanup(request, HT_IGNORE);
! 516: else
! 517: HTTPCleanup(request, HT_INTERRUPTED);
! 518: return HT_OK;
! 519: } else
! 520: http = (http_info *) net->context; /* Get existing copy */
1.71 frystyk 521:
522: /* Now jump into the machine. We know the state from the previous run */
523: while (1) {
524: switch (http->state) {
525: case HTTP_BEGIN:
526: /*
527: ** Compose authorization information (this was moved here
528: ** from after the making of the connection so that the connection
529: ** wouldn't have to wait while prompting username and password
530: ** from the user). -- AL 13.10.93
531: */
532: HTAA_composeAuth(request);
533: if (PROT_TRACE) {
534: if (request->authorization)
1.78 frystyk 535: fprintf(TDEST, "HTTP........ Sending Authorization: %s\n",
1.71 frystyk 536: request->authorization);
537: else
1.78 frystyk 538: fprintf(TDEST,
1.71 frystyk 539: "HTTP........ Not sending authorization (yet)\n");
540: }
541: http->state = HTTP_NEED_CONNECTION;
542: break;
543:
544: case HTTP_NEED_CONNECTION: /* Now let's set up a connection */
1.94 ! frystyk 545: status = HTDoConnect(net, HTAnchor_physical(request->anchor),
! 546: HTTP_PORT, NULL, NO);
1.80 frystyk 547: if (status == HT_OK) {
1.71 frystyk 548: if (PROT_TRACE)
1.78 frystyk 549: fprintf(TDEST, "HTTP........ Connected, socket %d\n",
1.94 ! frystyk 550: net->sockfd);
1.80 frystyk 551:
1.88 frystyk 552: /* Set up stream TO network */
553: request->input_stream =
1.94 ! frystyk 554: HTTPRequest_new(request, HTWriter_new(net, YES));
1.88 frystyk 555:
556: /*
557: ** Set up concurrent read/write if this request isn't the
558: ** source for a PUT or POST. As source we don't start reading
559: ** before all destinations are ready. If destination then
560: ** register the input stream and get ready for read
561: */
562: if (HTRequest_isPostWeb(request)) {
1.94 ! frystyk 563: HTEvent_Register(net->sockfd, request, (SockOps) FD_READ,
! 564: HTLoadHTTP, net->priority);
1.88 frystyk 565: HTRequest_linkDestination(request);
566: }
567:
568: /* Set up stream FROM network and corresponding read buffer */
1.94 ! frystyk 569: net->isoc = HTInputSocket_new(net->sockfd);
! 570: net->target = HTImProxy ?
1.80 frystyk 571: request->output_stream : HTTPStatus_new(request, http);
1.88 frystyk 572:
1.71 frystyk 573: http->state = HTTP_NEED_REQUEST;
574: } else if (status == HT_WOULD_BLOCK)
1.94 ! frystyk 575: return HT_OK;
1.71 frystyk 576: else
577: http->state = HTTP_ERROR; /* Error or interrupt */
578: break;
579:
1.87 frystyk 580: /* As we can do simultanous read and write this is now one state */
1.80 frystyk 581: case HTTP_NEED_REQUEST:
1.94 ! frystyk 582: if (ops == FD_WRITE || ops == FD_NONE) {
1.88 frystyk 583: if (HTRequest_isDestination(request)) {
1.94 ! frystyk 584: HTNet *srcnet = request->source->net;
! 585: HTEvent_Register(srcnet->sockfd, request->source,
! 586: (SockOps) FD_READ,
! 587: HTLoadHTTP, srcnet->priority);
! 588: return HT_OK;
1.80 frystyk 589: }
1.88 frystyk 590: status = request->PostCallBack ?
591: request->PostCallBack(request, request->input_stream) :
592: (*request->input_stream->isa->flush)(request->input_stream);
1.71 frystyk 593: if (status == HT_WOULD_BLOCK)
1.94 ! frystyk 594: return HT_OK;
! 595: else
! 596: ops = FD_READ; /* Trick to ensure that we do READ */
! 597: } else if (ops == FD_READ) {
! 598: status = HTSocketRead(request, net->target);
1.80 frystyk 599: if (status == HT_WOULD_BLOCK)
1.94 ! frystyk 600: return HT_OK;
! 601: else if (status == HT_LOADED)
! 602: http->state = http->next; /* Jump to next state */
1.91 frystyk 603: else if (status == HT_RELOAD)
604: http->state = HTTP_EXPIRED;
1.94 ! frystyk 605: else
! 606: http->state = HTTP_ERROR;
1.90 frystyk 607: } else {
1.78 frystyk 608: http->state = HTTP_ERROR;
1.90 frystyk 609: }
1.71 frystyk 610: break;
1.80 frystyk 611:
1.91 frystyk 612: case HTTP_NOT_MODIFIED:
613: http->state = HTTP_ERROR;
614: break;
615:
616: case HTTP_EXPIRED:
617: /* Dirty hack and fall through */
618: request->redirect = request->anchor->address;
619:
1.80 frystyk 620: case HTTP_REDIRECTION:
1.88 frystyk 621: /* Clean up the other connections or just this one */
622: if (HTRequest_isPostWeb(request))
623: HTRequest_killPostWeb(request);
624: else
1.94 ! frystyk 625: HTTPCleanup(request, HT_IGNORE);
1.88 frystyk 626:
627: /* If we found a new URL in the response */
1.71 frystyk 628: if (request->redirect) {
629: if (status == 301) {
630: HTErrorAdd(request, ERR_INFO, NO, HTERR_MOVED,
631: (void *) request->redirect,
632: (int) strlen(request->redirect), "HTLoadHTTP");
633: } else if (status == 302) {
634: HTErrorAdd(request, ERR_INFO, NO, HTERR_FOUND,
635: (void *) request->redirect,
636: (int) strlen(request->redirect), "HTLoadHTTP");
1.55 frystyk 637: }
1.88 frystyk 638:
639: /* If we haven't reached the limit for redirection */
1.94 ! frystyk 640: if (HTRequest_retry(request)) {
1.87 frystyk 641: HTAnchor *anchor = HTAnchor_findAddress(request->redirect);
1.88 frystyk 642: if (HTRequest_isPostWeb(request)) {
643: HTRequest *dest = HTRequest_mainDestination(request);
644: char *msg=(char*)malloc(strlen(request->redirect)+100);
1.87 frystyk 645: if (!msg) outofmem(__FILE__, "HTLoadHTTP");
1.88 frystyk 646: sprintf(msg, "\nLocation of %s has changed to %s for method %s, continue operation?",
647: HTRequest_isDestination(request) ?
648: "destination" : "source", request->redirect,
649: HTMethod_name(request->method));
1.92 frystyk 650: if (HTConfirm(request, msg)) {
1.87 frystyk 651: free(msg);
1.88 frystyk 652:
653: /* The new anchor inherits the Post Web */
1.91 frystyk 654: HTAnchor_moveAllLinks((HTAnchor *) request->anchor,
655: anchor);
1.88 frystyk 656: if (HTRequest_isSource(request))
657: HTRequest_delete(request);
1.94 ! frystyk 658: return HTCopyAnchor((HTAnchor *) anchor, dest) ?
! 659: HT_OK : HT_ERROR;
1.87 frystyk 660: }
661: free(msg);
1.94 ! frystyk 662: return HT_OK;
1.88 frystyk 663: } if (request->PostCallBack) {
1.87 frystyk 664: #if 0
1.94 ! frystyk 665: return HTUploadAnchor((HTAnchor*) anchor, request) ?
! 666: HT_OK : HT_ERROR;
1.87 frystyk 667: #endif
668: } else {
1.94 ! frystyk 669: return HTLoadAnchor((HTAnchor *) anchor, request) ?
! 670: HT_OK : HT_ERROR;
1.87 frystyk 671: }
1.71 frystyk 672: } else {
673: HTErrorAdd(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
1.58 frystyk 674: NULL, 0, "HTLoadHTTP");
1.88 frystyk 675: if (HTRequest_isPostWeb(request)) {
676: BOOL main = HTRequest_isMainDestination(request);
1.91 frystyk 677: if (HTRequest_isDestination(request)) {
678: HTLink *link =
679: HTAnchor_findLink((HTAnchor *) request->source->anchor,
680: (HTAnchor *) request->anchor);
681: HTAnchor_setLinkResult(link, HT_LINK_ERROR);
682: }
1.94 ! frystyk 683: HTNet_callback(request, main ? HT_ERROR : HT_IGNORE);
1.88 frystyk 684: HTRequest_removeDestination(request);
685: }
1.94 ! frystyk 686: return HT_OK;
1.58 frystyk 687: }
1.71 frystyk 688: } else {
689: HTErrorAdd(request, ERR_FATAL, NO, HTERR_BAD_REPLY,
690: NULL, 0, "HTLoadHTTP");
1.94 ! frystyk 691: return HT_OK;
1.71 frystyk 692: }
693: break;
1.80 frystyk 694:
1.71 frystyk 695: case HTTP_AA:
1.88 frystyk 696: /* Clean up the other connections or just this one */
697: if (HTRequest_isPostWeb(request))
698: HTRequest_killPostWeb(request);
699: else
1.94 ! frystyk 700: HTTPCleanup(request, HT_IGNORE);
1.88 frystyk 701:
702: /* Ask the user for a UserID and a passwd */
703: if (HTTPAuthentication(request) && HTAA_retryWithAuth(request)) {
1.94 ! frystyk 704: int ret;
1.88 frystyk 705: if (HTRequest_isPostWeb(request)) {
706: HTRequest *dest = HTRequest_mainDestination(request);
707: HTAnchor_appendMethods(request->anchor, request->method);
1.94 ! frystyk 708: ret=HTCopyAnchor((HTAnchor*)request->source->anchor, dest);
! 709: return ret ? HT_OK : HT_ERROR;
1.85 frystyk 710: } else if (request->PostCallBack) {
711: #if 0
1.94 ! frystyk 712: ret = HTUploadAnchor((HTAnchor*) request->anchor,request);
! 713: return ret ? HT_OK : HT_ERROR;
1.85 frystyk 714: #endif
715: } else {
1.94 ! frystyk 716: ret = HTLoadAnchor((HTAnchor *) request->anchor, request);
! 717: return ret ? HT_OK : HT_ERROR;
1.85 frystyk 718: }
1.88 frystyk 719: } else { /* If the guy said no :-( */
1.71 frystyk 720: HTErrorAdd(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
1.94 ! frystyk 721: NULL, 0, "HTLoadHTTP");
1.88 frystyk 722: if (HTRequest_isPostWeb(request)) {
723: BOOL main = HTRequest_isMainDestination(request);
1.91 frystyk 724: if (HTRequest_isDestination(request)) {
725: HTLink *link =
726: HTAnchor_findLink((HTAnchor *) request->source->anchor,
727: (HTAnchor *) request->anchor);
728: HTAnchor_setLinkResult(link, HT_LINK_ERROR);
729: }
1.94 ! frystyk 730: HTNet_callback(request, main ? HT_ERROR : HT_IGNORE);
1.88 frystyk 731: HTRequest_removeDestination(request);
732: }
1.94 ! frystyk 733: return HT_OK;
1.71 frystyk 734: }
735: break;
1.80 frystyk 736:
1.71 frystyk 737: case HTTP_GOT_DATA:
1.88 frystyk 738: if (HTRequest_isPostWeb(request)) {
739: BOOL main = HTRequest_isMainDestination(request);
1.91 frystyk 740: if (HTRequest_isDestination(request)) {
741: HTLink *link =
742: HTAnchor_findLink((HTAnchor *) request->source->anchor,
743: (HTAnchor *) request->anchor);
744: HTAnchor_setLinkResult(link, HT_LINK_OK);
745: }
1.88 frystyk 746: HTRequest_removeDestination(request);
1.94 ! frystyk 747: HTTPCleanup(request, main ? HT_LOADED : HT_IGNORE);
! 748: } else
! 749: HTTPCleanup(request, HT_LOADED);
! 750: return HT_OK;
1.71 frystyk 751: break;
752:
753: case HTTP_NO_DATA:
1.88 frystyk 754: if (HTRequest_isPostWeb(request)) {
755: BOOL main = HTRequest_isMainDestination(request);
1.91 frystyk 756: if (HTRequest_isDestination(request)) {
757: HTLink *link =
758: HTAnchor_findLink((HTAnchor *) request->source->anchor,
759: (HTAnchor *) request->anchor);
760: HTAnchor_setLinkResult(link, HT_LINK_OK);
761: }
1.88 frystyk 762: HTRequest_removeDestination(request);
1.94 ! frystyk 763: HTTPCleanup(request, main ? HT_NO_DATA : HT_IGNORE);
! 764: } else
! 765: HTTPCleanup(request, HT_NO_DATA);
! 766: return HT_OK;
1.71 frystyk 767: break;
1.80 frystyk 768:
1.94 ! frystyk 769: case HTTP_RETRY:
1.88 frystyk 770: if (HTRequest_isPostWeb(request)) {
771: BOOL main = HTRequest_isMainDestination(request);
1.91 frystyk 772: HTRequest_killPostWeb(request);
773: if (HTRequest_isDestination(request)) {
774: HTLink *link =
775: HTAnchor_findLink((HTAnchor*) request->source->anchor,
776: (HTAnchor*) request->anchor);
777: HTAnchor_setLinkResult(link, HT_LINK_ERROR);
778: }
1.88 frystyk 779: HTRequest_removeDestination(request);
1.94 ! frystyk 780: HTTPCleanup(request, main ? HT_RETRY : HT_IGNORE);
1.91 frystyk 781: } else
1.94 ! frystyk 782: HTTPCleanup(request, HT_RETRY);
! 783: return HT_OK;
1.81 frystyk 784: break;
785:
1.71 frystyk 786: case HTTP_ERROR:
1.88 frystyk 787: /* Clean up the other connections or just this one */
788: if (HTRequest_isPostWeb(request)) {
1.91 frystyk 789: BOOL main = HTRequest_isMainDestination(request);
1.88 frystyk 790: HTRequest_killPostWeb(request);
1.91 frystyk 791: if (HTRequest_isDestination(request)) {
792: HTLink *link =
793: HTAnchor_findLink((HTAnchor *) request->source->anchor,
794: (HTAnchor *) request->anchor);
795: HTAnchor_setLinkResult(link, HT_LINK_ERROR);
796: }
797: HTRequest_removeDestination(request);
1.94 ! frystyk 798: HTTPCleanup(request, main ? HT_ERROR : HT_IGNORE);
1.88 frystyk 799: } else
1.94 ! frystyk 800: HTTPCleanup(request, HT_ERROR);
! 801: return HT_OK;
1.71 frystyk 802: break;
803: }
804: } /* End of while(1) */
805: }
1.88 frystyk 806:
1.21 luotonen 807:
Webmaster