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