Annotation of libwww/Library/src/HTTP.c, revision 1.182
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.
1.182 ! frystyk 6: ** @(#) $Id: HTTP.c,v 1.181 1999/03/31 20:13:22 frystyk Exp $
1.74 frystyk 7: **
8: ** This module implments the HTTP protocol as a state machine
1.55 frystyk 9: **
10: ** History:
1.59 frystyk 11: ** < May 24 94 ?? Unknown - but obviously written
1.56 frystyk 12: ** May 24 94 HF Made reentrent and cleaned up a bit. Implemented
13: ** Forward, redirection, error handling and referer field
1.67 duns 14: ** 8 Jul 94 FM Insulate free() from _free structure element.
1.71 frystyk 15: ** Jul 94 HFN Written on top of HTTP.c, Henrik Frystyk
1.55 frystyk 16: **
1.1 timbl 17: */
18:
1.78 frystyk 19: /* Library include files */
1.161 frystyk 20: #include "wwwsys.h"
1.123 frystyk 21: #include "WWWUtil.h"
22: #include "WWWCore.h"
1.177 frystyk 23: #include "HTHeader.h"
24: #include "HTMIMERq.h"
1.94 frystyk 25: #include "HTReqMan.h"
1.179 frystyk 26: #include "HTNetMan.h"
1.109 frystyk 27: #include "HTTPUtil.h"
1.80 frystyk 28: #include "HTTPReq.h"
1.55 frystyk 29: #include "HTTP.h" /* Implements */
30:
31: /* Macros and other defines */
1.94 frystyk 32: #ifndef HTTP_PORT
33: #define HTTP_PORT 80 /* Allocated to http by Jon Postel/ISI 24-Jan-92 */
34: #endif
35:
1.71 frystyk 36: #define PUTC(c) (*me->target->isa->put_character)(me->target, c)
37: #define PUTS(s) (*me->target->isa->put_string)(me->target, s)
38: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
39: #define FREE_TARGET (*me->target->isa->_free)(me->target)
1.74 frystyk 40: #define ABORT_TARGET (*me->target->isa->abort)(me->target, e)
1.2 timbl 41:
1.178 frystyk 42: #ifdef HTDEBUG
1.177 frystyk 43: #include "WWWStream.h"
1.147 frystyk 44: #define HTTP_OUTPUT "w3chttp.out"
1.162 frystyk 45: PRIVATE FILE * htfp = NULL;
1.140 frystyk 46: #endif
1.139 frystyk 47:
1.59 frystyk 48: /* Type definitions and global variables etc. local to this module */
1.94 frystyk 49:
50: /* Final states have negative value */
1.59 frystyk 51: typedef enum _HTTPState {
1.167 frystyk 52: HTTP_KILL_PIPE = -4,
1.141 frystyk 53: HTTP_RECOVER_PIPE = -3,
1.134 frystyk 54: HTTP_ERROR = -2,
55: HTTP_OK = -1,
1.71 frystyk 56: HTTP_BEGIN = 0,
1.144 frystyk 57: HTTP_NEED_STREAM,
1.141 frystyk 58: HTTP_CONNECTED
1.59 frystyk 59: } HTTPState;
1.55 frystyk 60:
1.94 frystyk 61: /* This is the context structure for the this module */
1.55 frystyk 62: typedef struct _http_info {
1.81 frystyk 63: HTTPState state; /* Current State of the connection */
64: HTTPState next; /* Next state */
1.134 frystyk 65: int result; /* Result to report to the after filter */
1.139 frystyk 66: BOOL lock; /* Block for writing */
1.141 frystyk 67: HTNet * net;
1.157 frystyk 68: HTRequest * request;
69: HTTimer * timer;
1.165 frystyk 70: BOOL usedTimer;
1.181 frystyk 71: BOOL repetitive_writing;
1.55 frystyk 72: } http_info;
73:
1.88 frystyk 74: #define MAX_STATUS_LEN 100 /* Max nb of chars to check StatusLine */
1.55 frystyk 75:
1.71 frystyk 76: struct _HTStream {
1.119 frystyk 77: const HTStreamClass * isa;
1.71 frystyk 78: HTStream * target;
1.157 frystyk 79: HTStream * info_target; /* For 100 codes */
1.71 frystyk 80: HTRequest * request;
81: http_info * http;
1.121 frystyk 82: HTEOLState state;
1.71 frystyk 83: BOOL transparent;
1.150 frystyk 84: BOOL cont;
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.146 frystyk 90: int startLen;/* buflen when put_block was called */
1.71 frystyk 91: };
1.21 luotonen 92:
1.123 frystyk 93: struct _HTInputStream {
94: const HTInputStreamClass * isa;
95: };
96:
1.169 frystyk 97: /* How long to wait before writing the body in PUT and POST requests */
98: #define DEFAULT_FIRST_WRITE_DELAY 2000
99: #define DEFAULT_SECOND_WRITE_DELAY 3000
1.181 frystyk 100: #define DEFAULT_REPEAT_WRITE 30
1.169 frystyk 101:
102: PRIVATE ms_t HTFirstWriteDelay = DEFAULT_FIRST_WRITE_DELAY;
103: PRIVATE ms_t HTSecondWriteDelay = DEFAULT_SECOND_WRITE_DELAY;
1.181 frystyk 104: PRIVATE ms_t HTRepeatWrite = DEFAULT_REPEAT_WRITE;
1.169 frystyk 105:
1.147 frystyk 106: #ifdef HT_NO_PIPELINING
1.156 frystyk 107: PRIVATE HTTPConnectionMode ConnectionMode = HTTP_11_NO_PIPELINING;
1.147 frystyk 108: #else
1.156 frystyk 109: #ifdef HT_MUX
110: PRIVATE HTTPConnectionMode ConnectionMode = HTTP_11_MUX;
111: #else
112: #ifdef HT_FORCE_10
113: PRIVATE HTTPConnectionMode ConnectionMode = HTTP_FORCE_10;
114: #else
115: PRIVATE HTTPConnectionMode ConnectionMode = HTTP_11_PIPELINING;
116: #endif
117: #endif
1.147 frystyk 118: #endif
119:
1.71 frystyk 120: /* ------------------------------------------------------------------------- */
121: /* Help Functions */
122: /* ------------------------------------------------------------------------- */
1.21 luotonen 123:
1.94 frystyk 124: /* HTTPCleanup
125: ** -----------
1.55 frystyk 126: ** This function closes the connection and frees memory.
1.94 frystyk 127: ** Returns YES on OK, else NO
1.1 timbl 128: */
1.94 frystyk 129: PRIVATE int HTTPCleanup (HTRequest *req, int status)
1.1 timbl 130: {
1.126 frystyk 131: HTNet * net = HTRequest_net(req);
132: http_info * http = (http_info *) HTNet_context(net);
133: HTStream * input = HTRequest_inputStream(req);
1.80 frystyk 134:
1.178 frystyk 135: HTTRACE(PROT_TRACE, "HTTP Clean.. Called with status %d, net %p\n" _ status _ net);
1.144 frystyk 136:
1.164 frystyk 137: if (status == HT_INTERRUPTED) {
138: HTAlertCallback * cbf = HTAlert_find(HT_PROG_INTERRUPT);
139: if (cbf) (*cbf)(req, HT_PROG_INTERRUPT,
140: HT_MSG_NULL, NULL, NULL, NULL);
1.166 frystyk 141: } else if (status == HT_TIMEOUT) {
142: HTAlertCallback * cbf = HTAlert_find(HT_PROG_TIMEOUT);
143: if (cbf) (*cbf)(req, HT_PROG_TIMEOUT,
144: HT_MSG_NULL, NULL, NULL, NULL);
1.164 frystyk 145: }
1.166 frystyk 146:
1.94 frystyk 147: /* Free stream with data TO network */
1.167 frystyk 148: if (input) {
1.166 frystyk 149: if (status==HT_INTERRUPTED || status==HT_RECOVER_PIPE || status==HT_TIMEOUT)
1.126 frystyk 150: (*input->isa->abort)(input, NULL);
1.94 frystyk 151: else
1.126 frystyk 152: (*input->isa->_free)(input);
153: HTRequest_setInputStream(req, NULL);
1.94 frystyk 154: }
1.88 frystyk 155:
1.144 frystyk 156: /*
1.157 frystyk 157: ** Remove if we have registered an upload function as a callback
158: */
159: if (http->timer) {
160: HTTimer_delete(http->timer);
161: http->timer = NULL;
162: }
163:
164: /*
1.144 frystyk 165: ** Remove the request object and our own context structure for http.
166: */
167: if (status != HT_RECOVER_PIPE) {
1.153 frystyk 168: HTNet_delete(net, status);
1.141 frystyk 169: HT_FREE(http);
170: }
1.94 frystyk 171: return YES;
1.55 frystyk 172: }
173:
1.71 frystyk 174: /*
1.130 frystyk 175: ** Informational 1xx codes are handled separately
1.136 frystyk 176: ** Returns YES if we should continue, NO if we should stop
1.55 frystyk 177: */
1.130 frystyk 178: PRIVATE BOOL HTTPInformation (HTStream * me)
1.55 frystyk 179: {
1.136 frystyk 180: http_info * http = me->http;
1.71 frystyk 181: switch (me->status) {
182:
1.136 frystyk 183: case 100:
1.161 frystyk 184: #if 0
1.136 frystyk 185: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_CONTINUE,
186: me->reason, (int) strlen(me->reason),
187: "HTTPInformation");
1.161 frystyk 188: #endif
1.182 ! frystyk 189: http->result = HT_CONTINUE;
1.136 frystyk 190: return YES;
191: break;
192:
1.130 frystyk 193: case 101:
1.136 frystyk 194: /*
195: ** We consider 101 Switching as a final state and exit this request
196: */
1.130 frystyk 197: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_SWITCHING,
1.127 frystyk 198: me->reason, (int) strlen(me->reason),
1.130 frystyk 199: "HTTPInformation");
1.136 frystyk 200: http->next = HTTP_OK;
201: http->result = HT_UPGRADE;
1.180 frystyk 202: return YES;
1.127 frystyk 203: break;
204:
1.130 frystyk 205: default:
1.136 frystyk 206: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_REPLY,
207: (void *) me->buffer, me->buflen, "HTTPNextState");
208: http->next = HTTP_ERROR;
209: http->result = HT_ERROR;
1.127 frystyk 210: break;
1.130 frystyk 211: }
1.136 frystyk 212: return NO;
1.130 frystyk 213: }
214:
1.158 frystyk 215:
216:
1.130 frystyk 217: /*
218: ** This is a big switch handling all HTTP return codes. It puts in any
219: ** appropiate error message and decides whether we should expect data
220: ** or not.
221: */
222: PRIVATE void HTTPNextState (HTStream * me)
223: {
1.134 frystyk 224: http_info * http = me->http;
1.158 frystyk 225: int error_class = me->status / 100;
226: switch (error_class) {
1.127 frystyk 227:
1.158 frystyk 228: case 0: /* 0.9 response */
229: case 2:
1.112 frystyk 230:
1.158 frystyk 231: switch (me->status) {
1.112 frystyk 232:
1.158 frystyk 233: case 201: /* Created */
234: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_CREATED,
235: me->reason, (int) strlen(me->reason),
236: "HTTPNextState");
237: http->next = HTTP_OK;
238: http->result = HT_CREATED;
239: break;
240:
241: case 202: /* Accepted */
242: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_ACCEPTED,
243: me->reason, (int) strlen(me->reason),
244: "HTTPNextState");
245: http->next = HTTP_OK;
246: http->result = HT_ACCEPTED;
247: break;
248:
249: case 203: /* Non-authoritative information */
250: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_NON_AUTHORITATIVE,
251: me->reason, (int) strlen(me->reason),
252: "HTTPNextState");
253: http->next = HTTP_OK;
254: http->result = HT_LOADED;
255: break;
256:
257: case 204: /* No Response */
258: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_NO_CONTENT,
259: me->reason, (int) strlen(me->reason),
260: "HTTPNextState");
261: http->next = HTTP_OK;
262: http->result = HT_NO_DATA;
263: break;
264:
265: case 205: /* Reset Content */
266: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_RESET,
267: me->reason, (int) strlen(me->reason),
268: "HTTPNextState");
269: http->next = HTTP_OK;
270: http->result = HT_RESET_CONTENT;
271: break;
272:
273: case 206: /* Partial Content */
274: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_PARTIAL,
275: me->reason, (int) strlen(me->reason),
276: "HTTPNextState");
277: http->next = HTTP_OK;
278: http->result = HT_PARTIAL_CONTENT;
279: break;
280:
281: case 207: /* Partial Update OK */
282: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_PARTIAL_OK,
283: me->reason, (int) strlen(me->reason),
284: "HTTPNextState");
285: http->next = HTTP_OK;
286: http->result = HT_PARTIAL_CONTENT;
287: break;
288:
289: default:
290: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_OK,
291: me->reason, (int) strlen(me->reason),
292: "HTTPNextState");
293: http->next = HTTP_OK;
294: http->result = HT_LOADED;
295: break;
296: }
1.134 frystyk 297: break;
298:
1.158 frystyk 299: case 3:
1.134 frystyk 300:
1.158 frystyk 301: switch (me->status) {
1.78 frystyk 302:
1.158 frystyk 303: case 301: /* Moved */
304: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_MOVED,
305: me->reason, (int) strlen(me->reason),
306: "HTTPNextState");
307: http->next = HTTP_ERROR;
308: http->result = HT_PERM_REDIRECT;
309: break;
310:
311: case 302: /* Found */
312: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_FOUND,
313: me->reason, (int) strlen(me->reason),
314: "HTTPNextState");
315: http->next = HTTP_ERROR;
316: http->result = HT_FOUND;
317: break;
1.55 frystyk 318:
1.158 frystyk 319: case 303: /* Method */
320: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_METHOD,
321: me->reason, (int) strlen(me->reason),
322: "HTTPNextState");
323: http->next = HTTP_ERROR;
324: http->result = HT_SEE_OTHER;
325: break;
326:
327: case 304: /* Not Modified */
328: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_NOT_MODIFIED,
329: me->reason, (int) strlen(me->reason),
330: "HTTPNextState");
331: http->next = HTTP_OK;
332: http->result = HT_NOT_MODIFIED;
333: break;
1.134 frystyk 334:
1.158 frystyk 335: case 305: /* Use proxy */
336: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_USE_PROXY,
337: me->reason, (int) strlen(me->reason),
338: "HTTPNextState");
339: http->next = HTTP_ERROR;
340: http->result = HT_USE_PROXY;
341: break;
1.55 frystyk 342:
1.152 frystyk 343: #if 0
1.158 frystyk 344: case 306: /* Use proxy */
345: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_PROXY_REDIRECT,
346: me->reason, (int) strlen(me->reason),
347: "HTTPNextState");
348: http->next = HTTP_ERROR;
349: http->result = HT_USE_PROXY;
350: break;
1.152 frystyk 351: #endif
352:
1.158 frystyk 353: case 307: /* Use proxy */
354: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_TEMP_REDIRECT,
355: me->reason, (int) strlen(me->reason),
356: "HTTPNextState");
357: http->next = HTTP_ERROR;
358: http->result = HT_TEMP_REDIRECT;
359: break;
360:
361: default:
362: HTRequest_addError(me->request, ERR_INFO, NO, HTERR_MULTIPLE,
363: me->reason, (int) strlen(me->reason),
364: "HTTPNextState");
365: http->next = HTTP_OK;
366: http->result = HT_LOADED;
367: break;
368: }
369: break;
370:
371: case 4:
372:
373: switch (me->status) {
374: case 401:
375: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
376: me->reason, (int) strlen(me->reason), "HTTPNextState");
377: http->next = HTTP_ERROR;
378: http->result = HT_NO_ACCESS;
379: break;
1.152 frystyk 380:
1.158 frystyk 381: case 402: /* Payment required */
382: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_PAYMENT_REQUIRED,
383: me->reason, (int) strlen(me->reason), "HTTPNextState");
384: http->next = HTTP_ERROR;
385: http->result = -402;
386: break;
1.71 frystyk 387:
1.158 frystyk 388: case 403: /* Forbidden */
389: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_FORBIDDEN,
390: me->reason, (int) strlen(me->reason), "HTTPNextState");
391: http->next = HTTP_ERROR;
392: http->result = HT_FORBIDDEN;
393: break;
1.55 frystyk 394:
1.158 frystyk 395: case 404: /* Not Found */
396: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NOT_FOUND,
397: me->reason, (int) strlen(me->reason), "HTTPNextState");
398: http->next = HTTP_ERROR;
399: http->result = -404;
400: break;
1.55 frystyk 401:
1.158 frystyk 402: case 405: /* Not Allowed */
403: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NOT_ALLOWED,
404: me->reason, (int) strlen(me->reason), "HTTPNextState");
405: http->next = HTTP_ERROR;
406: http->result = -405;
407: break;
408:
409: case 406: /* None Acceptable */
410: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NONE_ACCEPTABLE,
411: me->reason, (int) strlen(me->reason), "HTTPNextState");
412: http->next = HTTP_ERROR;
413: http->result = HT_NOT_ACCEPTABLE;
414: break;
415:
416: case 407: /* Proxy Authentication Required */
417: HTRequest_addError(me->request, ERR_FATAL, NO,HTERR_PROXY_UNAUTHORIZED,
418: me->reason, (int) strlen(me->reason), "HTTPNextState");
419: http->next = HTTP_ERROR;
420: http->result = HT_NO_PROXY_ACCESS;
421: break;
422:
423: case 408: /* Request Timeout */
424: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_TIMEOUT,
425: me->reason, (int) strlen(me->reason), "HTTPNextState");
426: http->next = HTTP_ERROR;
427: http->result = -408;
428: break;
429:
430: case 409: /* Conflict */
431: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_CONFLICT,
432: me->reason, (int) strlen(me->reason), "HTTPNextState");
433: http->next = HTTP_ERROR;
434: http->result = HT_CONFLICT;
435: break;
436:
437: case 410: /* Gone */
438: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_GONE,
439: me->reason, (int) strlen(me->reason), "HTTPNextState");
440: http->next = HTTP_ERROR;
441: http->result = -410;
442: break;
443:
444: case 411: /* Length Required */
445: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_LENGTH_REQUIRED,
446: me->reason, (int) strlen(me->reason), "HTTPNextState");
447: http->next = HTTP_ERROR;
448: http->result = HT_LENGTH_REQUIRED;
449: break;
450:
451: case 412: /* Precondition failed */
452: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_PRECON_FAILED,
453: me->reason, (int) strlen(me->reason), "HTTPNextState");
454: http->next = HTTP_ERROR;
455: http->result = -412;
456: break;
457:
458: case 413: /* Request entity too large */
459: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_TOO_BIG,
460: me->reason, (int) strlen(me->reason), "HTTPNextState");
461: http->next = HTTP_ERROR;
462: http->result = -413;
463: break;
464:
465: case 414: /* Request-URI too long */
466: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_URI_TOO_BIG,
467: me->reason, (int) strlen(me->reason), "HTTPNextState");
468: http->next = HTTP_ERROR;
469: http->result = -414;
470: break;
471:
472: case 415: /* Unsupported */
473: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_UNSUPPORTED,
474: me->reason, (int) strlen(me->reason), "HTTPNextState");
475: http->next = HTTP_ERROR;
476: http->result = -415;
477: break;
478:
479: case 416: /* Request Range not satisfiable */
480: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_RANGE,
481: me->reason, (int) strlen(me->reason), "HTTPNextState");
482: http->next = HTTP_ERROR;
483: http->result = -416;
484: break;
485:
486: case 417: /* Expectation Failed */
487: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_EXPECTATION_FAILED,
488: me->reason, (int) strlen(me->reason), "HTTPNextState");
489: http->next = HTTP_ERROR;
490: http->result = -417;
491: break;
492:
493: case 418: /* Reauthentication required */
494: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_REAUTH,
495: me->reason, (int) strlen(me->reason), "HTTPNextState");
496: http->next = HTTP_ERROR;
497: http->result = -418;
498: break;
499:
500: case 419: /* Proxy Reauthentication required */
501: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_PROXY_REAUTH,
502: me->reason, (int) strlen(me->reason), "HTTPNextState");
503: http->next = HTTP_ERROR;
504: http->result = -419;
505: break;
506:
507: default:
508: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_REQUEST,
509: me->reason, (int) strlen(me->reason), "HTTPNextState");
510: http->next = HTTP_ERROR;
511: http->result = -400;
512: break;
513: }
1.134 frystyk 514: break;
515:
1.158 frystyk 516: case 5:
1.134 frystyk 517:
1.158 frystyk 518: switch (me->status) {
519: case 501:
520: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NOT_IMPLEMENTED,
521: me->reason, (int) strlen(me->reason), "HTTPNextState");
522: http->next = HTTP_ERROR;
523: http->result = -501;
524: break;
525:
526: case 502:
527: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_GATE,
528: me->reason, (int) strlen(me->reason), "HTTPNextState");
529: http->next = HTTP_ERROR;
530: http->result = -502;
531: break;
532:
533: case 503:
534: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_DOWN,
535: me->reason, (int) strlen(me->reason), "HTTPNextState");
536: http->next = HTTP_ERROR;
537:
538: /*
539: ** If Retry-After header is found then return HT_RETRY else HT_ERROR.
540: ** The caller may want to reissue the request at a later point in time.
541: */
542: {
543: HTResponse * response = HTRequest_response(me->request);
544: if (HTResponse_retryTime(response))
545: http->result = HT_RETRY;
546: else
547: http->result = -500;
548: }
549: break;
550:
551: case 504:
552: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_GATE_TIMEOUT,
553: me->reason, (int) strlen(me->reason), "HTTPNextState");
554: http->next = HTTP_ERROR;
555: http->result = -504;
556: break;
557:
558: case 505: /* Unsupported protocol version */
559: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_VERSION,
560: me->reason, (int) strlen(me->reason), "HTTPNextState");
561: http->next = HTTP_ERROR;
562: http->result = HT_BAD_VERSION;
563: break;
564:
565: case 506: /* Partial update Not Implemented */
566: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_NO_PARTIAL_UPDATE,
567: me->reason, (int) strlen(me->reason), "HTTPNextState");
568: http->next = HTTP_ERROR;
569: http->result = HT_BAD_VERSION;
570: break;
571:
572: default: /* bad number */
573: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_INTERNAL,
574: me->reason, (int) strlen(me->reason), "HTTPNextState");
575: http->next = HTTP_ERROR;
576: http->result = -500;
577: break;
1.140 frystyk 578: }
1.78 frystyk 579: break;
580:
1.158 frystyk 581: default:
1.104 frystyk 582: HTRequest_addError(me->request, ERR_FATAL, NO, HTERR_BAD_REPLY,
1.158 frystyk 583: (void *) me->buffer, me->buflen, "HTTPNextState");
1.134 frystyk 584: http->next = HTTP_ERROR;
1.155 frystyk 585: http->result = -(me->status);
1.71 frystyk 586: break;
1.55 frystyk 587: }
588: }
589:
1.71 frystyk 590: /* ------------------------------------------------------------------------- */
591: /* HTTP Status Line Stream */
592: /* ------------------------------------------------------------------------- */
1.55 frystyk 593:
1.71 frystyk 594: /*
1.141 frystyk 595: ** Analyze the stream we have read. If it is a HTTP 1.0 or higher
1.71 frystyk 596: ** then create a MIME-stream, else create a Guess stream to find out
597: ** what the 0.9 server is sending. We need to copy the buffer as we don't
598: ** know if we can modify the contents or not.
1.78 frystyk 599: **
600: ** Stream handling is a function of the status code returned from the
601: ** server:
602: ** 200: Use `output_stream' in HTRequest structure
1.94 frystyk 603: ** else: Use `debug_stream' in HTRequest structure
1.80 frystyk 604: **
605: ** Return: YES if buffer should be written out. NO otherwise
1.56 frystyk 606: */
1.157 frystyk 607: PRIVATE int stream_pipe (HTStream * me, int length)
1.56 frystyk 608: {
1.136 frystyk 609: HTRequest * request = me->request;
610: HTNet * net = HTRequest_net(request);
1.123 frystyk 611: HTHost * host = HTNet_host(net);
1.141 frystyk 612:
1.153 frystyk 613: #if 0
614: {
615: char * uri = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
616: fprintf(stderr, "HTTP header: %s for '%s'\n", me->buffer, uri);
617: HT_FREE(uri);
618: }
619: #endif
620:
1.132 frystyk 621: /*
622: ** Just check for HTTP and not HTTP/ as NCSA server chokes on 1.1 replies
623: ** Thanks to Markku Savela <msa@msa.tte.vtt.fi>
1.141 frystyk 624:
1.132 frystyk 625: */
626: if (strncasecomp(me->buffer, "http", 4)) {
1.80 frystyk 627: int status;
1.136 frystyk 628: HTRequest_addError(request, ERR_INFO, NO, HTERR_HTTP09,
1.80 frystyk 629: (void *) me->buffer, me->buflen, "HTTPStatusStream");
1.136 frystyk 630: me->target = HTStreamStack(WWW_UNKNOWN,
631: HTRequest_outputFormat(request),
632: HTRequest_outputStream(request),
633: request, NO);
1.134 frystyk 634: me->http->next = HTTP_OK;
1.80 frystyk 635: if ((status = PUTBLOCK(me->buffer, me->buflen)) == HT_OK)
636: me->transparent = YES;
1.123 frystyk 637: HTHost_setVersion(host, HTTP_09);
1.157 frystyk 638: if (length > 0) HTHost_setConsumed(host, length);
1.178 frystyk 639: HTTRACE(PROT_TRACE, "HTTP Status. `%s\' is probably a broken 1.0 server that doesn't understand HEAD\n" _
1.171 frystyk 640: HTHost_name(host));
641: return HT_ERROR;
1.80 frystyk 642: } else {
1.140 frystyk 643: HTResponse * response = HTRequest_response(request);
1.155 frystyk 644: char * ptr = me->buffer+5; /* Skip the HTTP part */
645: char * vptr = NULL;
646: int major = 0;
647: int minor = 0;
648: me->version = vptr = HTNextField(&ptr);
649: if (vptr) {
650: major = (int) strtol(me->version, &vptr, 10);
651: if (vptr++) minor = strtol(vptr, NULL, 10);
652: }
1.95 frystyk 653:
1.130 frystyk 654: /* Here we want to find out when to use persistent connection */
1.171 frystyk 655: if (major > 1 && major < 100) {
1.178 frystyk 656: HTTRACE(PROT_TRACE, "HTTP Status. Major version number is %d\n" _ major);
1.155 frystyk 657: me->target = HTErrorStream();
1.158 frystyk 658: me->status = 9999;
1.155 frystyk 659: HTTPNextState(me); /* Get next state */
1.158 frystyk 660: return HT_ERROR;
1.155 frystyk 661: } else if (minor <= 0) {
1.171 frystyk 662: if (major > 100) {
1.178 frystyk 663: HTTRACE(PROT_TRACE, "HTTP Status. This is a *BROKEN* HTTP/1.0 server\n");
1.171 frystyk 664: me->status = 200;
665: } else {
1.178 frystyk 666: HTTRACE(PROT_TRACE, "HTTP Status. This is an HTTP/1.0 server\n");
1.171 frystyk 667: me->status = atoi(HTNextField(&ptr));
668: }
1.128 frystyk 669: HTHost_setVersion(host, HTTP_10);
1.155 frystyk 670: } else { /* 1.x, x>0 family */
671: HTHost_setVersion(host, HTTP_11); /* Best we can do */
1.156 frystyk 672: if (ConnectionMode & HTTP_11_NO_PIPELINING) {
1.178 frystyk 673: HTTRACE(PROT_TRACE, "HTTP........ Mode is HTTP/1.1 with NO PIPELINING\n");
1.147 frystyk 674: HTNet_setPersistent(net, YES, HT_TP_SINGLE);
1.156 frystyk 675: } else if (ConnectionMode & HTTP_11_MUX) {
1.178 frystyk 676: HTTRACE(PROT_TRACE, "HTTP........ Mode is HTTP/1.1 with MUXING\n");
1.156 frystyk 677: HTNet_setPersistent(net, YES, HT_TP_INTERLEAVE);
1.147 frystyk 678: } else if (ConnectionMode & HTTP_FORCE_10) {
1.178 frystyk 679: HTTRACE(PROT_TRACE, "HTTP........ Mode is FORCE HTTP/1.0\n");
1.147 frystyk 680: HTHost_setVersion(host, HTTP_10);
681: HTNet_setPersistent(net, NO, HT_TP_SINGLE);
682: } else
683: HTNet_setPersistent(net, YES, HT_TP_PIPELINE);
1.171 frystyk 684: me->status = atoi(HTNextField(&ptr));
1.128 frystyk 685: }
1.95 frystyk 686:
1.81 frystyk 687: me->reason = ptr;
688: if ((ptr = strchr(me->reason, '\r')) != NULL) /* Strip \r and \n */
689: *ptr = '\0';
690: else if ((ptr = strchr(me->reason, '\n')) != NULL)
691: *ptr = '\0';
692:
1.130 frystyk 693: /*
694: ** If it is a 1xx code then find out what to do and return until we
695: ** get the next code. In the case of Upgrade we may not get back here
696: ** at all. If we are uploading an entity then continue doing that
697: */
698: if (me->status/100 == 1) {
1.136 frystyk 699: if (HTTPInformation(me) == YES) {
1.180 frystyk 700: if (me->status==100) {
701: me->buflen = 0;
702: me->state = EOL_BEGIN;
703: if (me->info_target) (*me->info_target->isa->_free)(me->info_target);
704: me->info_target = HTStreamStack(WWW_MIME_CONT,
705: HTRequest_debugFormat(request),
706: HTRequest_debugStream(request),
707: request, NO);
708: if (length > 0) HTHost_setConsumed(host, length);
709: return HT_OK;
710: } else if (me->status==101) {
711: if (me->info_target) (*me->info_target->isa->_free)(me->info_target);
712: me->target = HTStreamStack(WWW_MIME_UPGRADE,
713: HTRequest_outputFormat(request),
714: HTRequest_outputStream(request),
715: request, NO);
716: if (length > 0) HTHost_setConsumed(host, length);
717: me->transparent = YES;
718: return HT_OK;
719: }
1.103 frystyk 720: }
1.56 frystyk 721: }
1.133 frystyk 722:
723: /*
724: ** As we are getting fresh metainformation in the HTTP response,
725: ** we clear the old metainfomation in order not to mix it with the new
726: ** one. This is particularly important for the content-length and the
1.139 frystyk 727: ** like. The TRACE and OPTIONS method just adds to the current
728: ** metainformation so in that case we don't clear the anchor.
1.133 frystyk 729: */
1.136 frystyk 730: if (me->status==200 || me->status==203 || me->status==300) {
1.140 frystyk 731: /*
732: ** 200, 203 and 300 are all fully cacheable responses. No byte
733: ** ranges or anything else make life hard in this case.
734: */
1.148 frystyk 735: HTAnchor_clearHeader(HTRequest_anchor(request));
1.166 frystyk 736: HTResponse_setCachable(response, HT_CACHE_ALL);
1.136 frystyk 737: me->target = HTStreamStack(WWW_MIME,
738: HTRequest_outputFormat(request),
739: HTRequest_outputStream(request),
740: request, NO);
1.176 frystyk 741: } else if (me->status==204) {
742: HTResponse_setCachable(response, HT_CACHE_ALL);
743: me->target = HTStreamStack(WWW_MIME_HEAD,
744: HTRequest_debugFormat(request),
745: HTRequest_debugStream(request),
746: request, NO);
1.140 frystyk 747: } else if (me->status==206) {
748: /*
749: ** We got a partial response and now we must check whether
750: ** we issued a cache If-Range request or it was a new
751: ** partial response which we don't have in cache. In the latter
752: ** case, we don't cache the object and in the former we append
753: ** the result to the already existing cache entry.
754: */
755: HTReload reload = HTRequest_reloadMode(request);
756: if (reload == HT_CACHE_RANGE_VALIDATE) {
1.166 frystyk 757: HTResponse_setCachable(response, HT_CACHE_ALL);
1.140 frystyk 758: me->target = HTStreamStack(WWW_MIME_PART,
759: HTRequest_outputFormat(request),
760: HTRequest_outputStream(request),
761: request, NO);
762: } else {
1.149 frystyk 763: HTAnchor_clearHeader(HTRequest_anchor(request));
1.140 frystyk 764: me->target = HTStreamStack(WWW_MIME,
765: HTRequest_outputFormat(request),
766: HTRequest_outputStream(request),
767: request, NO);
768: }
1.176 frystyk 769: } else if (me->status==304) {
770: HTResponse_setCachable(response, HT_CACHE_NOT_MODIFIED);
1.136 frystyk 771: me->target = HTStreamStack(WWW_MIME_HEAD,
772: HTRequest_debugFormat(request),
773: HTRequest_debugStream(request),
774: request, NO);
775: } else if (HTRequest_debugStream(request)) {
1.175 frystyk 776: HTResponse_setCachable(response,
777: (me->status == 201) ? HT_CACHE_ETAG : HT_NO_CACHE);
1.136 frystyk 778: me->target = HTStreamStack(WWW_MIME,
779: HTRequest_debugFormat(request),
780: HTRequest_debugStream(request),
781: request, NO);
782: } else {
1.140 frystyk 783: /*
784: ** We still need to parse the MIME part in order to find any
785: ** valuable meta information which is needed from the response.
786: */
1.175 frystyk 787: HTResponse_setCachable(response,
788: (me->status == 201) ? HT_CACHE_ETAG : HT_NO_CACHE);
1.136 frystyk 789: me->target = HTStreamStack(WWW_MIME,
790: HTRequest_debugFormat(request),
791: HTRequest_debugStream(request),
792: request, NO);
1.133 frystyk 793: }
1.56 frystyk 794: }
1.113 frystyk 795: if (!me->target) me->target = HTErrorStream();
1.81 frystyk 796: HTTPNextState(me); /* Get next state */
1.80 frystyk 797: me->transparent = YES;
1.157 frystyk 798: if (length > 0) HTHost_setConsumed(HTNet_host(HTRequest_net(me->request)), length);
1.80 frystyk 799: return HT_OK;
1.71 frystyk 800: }
1.56 frystyk 801:
1.80 frystyk 802: /*
803: ** Searches for HTTP header line until buffer fills up or a CRLF or LF
804: ** is found
805: */
1.119 frystyk 806: PRIVATE int HTTPStatus_put_block (HTStream * me, const char * b, int l)
1.71 frystyk 807: {
1.150 frystyk 808: int status = HT_OK;
1.153 frystyk 809: int length = l;
1.146 frystyk 810: me->startLen = me->buflen;
1.80 frystyk 811: while (!me->transparent && l-- > 0) {
1.157 frystyk 812: if (me->info_target) {
813:
814: /* Put data down the 1xx return code parser until we are done. */
815: status = (*me->info_target->isa->put_block)(me->info_target, b, l+1);
816: if (status != HT_CONTINUE) return status;
817:
818: /* Now free the info stream */
819: (*me->info_target->isa->_free)(me->info_target);
820: me->info_target = NULL;
821:
822: /* Update where we are in the stream */
823: l = HTHost_remainingRead(HTNet_host(HTRequest_net(me->request)));
1.163 frystyk 824: b += (length-l);
1.159 frystyk 825: length = l;
1.163 frystyk 826: if (l <= 0) break;
1.157 frystyk 827:
1.80 frystyk 828: } else {
829: *(me->buffer+me->buflen++) = *b;
830: if (me->state == EOL_FCR) {
831: if (*b == LF) { /* Line found */
1.157 frystyk 832: if ((status = stream_pipe(me, length-l)) != HT_OK) return status;
1.80 frystyk 833: } else {
834: me->state = EOL_BEGIN;
835: }
836: } else if (*b == CR) {
837: me->state = EOL_FCR;
838: } else if (*b == LF) {
1.157 frystyk 839: if ((status = stream_pipe(me, length-l)) != HT_OK) return status;
1.71 frystyk 840: } else {
1.80 frystyk 841: if (me->buflen >= MAX_STATUS_LEN) {
1.157 frystyk 842: if ((status = stream_pipe(me, length-l)) != HT_OK) return status;
1.80 frystyk 843: }
1.71 frystyk 844: }
1.80 frystyk 845: b++;
1.71 frystyk 846: }
1.56 frystyk 847: }
1.153 frystyk 848:
1.159 frystyk 849: if (!me->transparent && length != l)
850: HTHost_setConsumed(HTNet_host(HTRequest_net(me->request)), length-l);
851:
1.99 frystyk 852: if (l > 0) return PUTBLOCK(b, l);
1.150 frystyk 853: return status;
1.56 frystyk 854: }
855:
1.119 frystyk 856: PRIVATE int HTTPStatus_put_string (HTStream * me, const char * s)
1.71 frystyk 857: {
1.80 frystyk 858: return HTTPStatus_put_block(me, s, (int) strlen(s));
1.71 frystyk 859: }
1.56 frystyk 860:
1.100 frystyk 861: PRIVATE int HTTPStatus_put_character (HTStream * me, char c)
1.71 frystyk 862: {
1.80 frystyk 863: return HTTPStatus_put_block(me, &c, 1);
864: }
865:
1.100 frystyk 866: PRIVATE int HTTPStatus_flush (HTStream * me)
1.80 frystyk 867: {
868: return (*me->target->isa->flush)(me->target);
1.71 frystyk 869: }
870:
1.100 frystyk 871: PRIVATE int HTTPStatus_free (HTStream * me)
1.71 frystyk 872: {
1.87 frystyk 873: int status = HT_OK;
874: if (me->target) {
875: if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
876: return HT_WOULD_BLOCK;
877: }
1.115 frystyk 878: HT_FREE(me);
1.100 frystyk 879: return status;
1.71 frystyk 880: }
881:
1.104 frystyk 882: PRIVATE int HTTPStatus_abort (HTStream * me, HTList * e)
1.71 frystyk 883: {
884: if (me->target)
1.74 frystyk 885: ABORT_TARGET;
1.115 frystyk 886: HT_FREE(me);
1.178 frystyk 887: HTTRACE(PROT_TRACE, "HTTPStatus.. ABORTING...\n");
1.80 frystyk 888: return HT_ERROR;
1.71 frystyk 889: }
890:
891: /* HTTPStatus Stream
892: ** -----------------
893: */
1.119 frystyk 894: PRIVATE const HTStreamClass HTTPStatusClass =
1.71 frystyk 895: {
896: "HTTPStatus",
1.80 frystyk 897: HTTPStatus_flush,
1.71 frystyk 898: HTTPStatus_free,
899: HTTPStatus_abort,
900: HTTPStatus_put_character,
901: HTTPStatus_put_string,
902: HTTPStatus_put_block
903: };
904:
1.113 frystyk 905: PUBLIC HTStream * HTTPStatus_new (HTRequest * request,
906: void * param,
907: HTFormat input_format,
908: HTFormat output_format,
909: HTStream * output_stream)
1.71 frystyk 910: {
1.115 frystyk 911: HTStream * me;
912: if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
913: HT_OUTOFMEM("HTTPStatus_new");
1.71 frystyk 914: me->isa = &HTTPStatusClass;
1.113 frystyk 915: if (request) {
1.126 frystyk 916: HTNet * net = HTRequest_net(request);
1.125 frystyk 917: /* Get existing copy */
918: http_info * http = (http_info *) HTNet_context(net);
1.113 frystyk 919: me->request = request;
920: me->http = http;
921: http->next = HTTP_ERROR;
922: me->state = EOL_BEGIN;
923: return me;
924: } else
925: return HTErrorStream();
1.71 frystyk 926: }
927:
928: /* ------------------------------------------------------------------------- */
929:
930: /* Load Document from HTTP Server HTLoadHTTP
1.55 frystyk 931: ** ==============================
932: **
933: ** Given a hypertext address, this routine loads a document.
934: **
935: ** On entry,
936: ** request This is the request structure
1.94 frystyk 937: ** returns HT_ERROR Error has occured in call back
938: ** HT_OK Call back was OK
1.55 frystyk 939: */
1.141 frystyk 940: PRIVATE int HTTPEvent (SOCKET soc, void * pVoid, HTEventType type);
941:
942: PUBLIC int HTLoadHTTP (SOCKET soc, HTRequest * request)
1.55 frystyk 943: {
944: http_info *http; /* Specific protocol information */
1.112 frystyk 945: HTParentAnchor *anchor = HTRequest_anchor(request);
1.141 frystyk 946: HTNet * net = HTRequest_net(request);
1.112 frystyk 947:
1.94 frystyk 948: /*
949: ** Initiate a new http structure and bind to request structure
950: ** This is actually state HTTP_BEGIN, but it can't be in the state
951: ** machine as we need the structure first.
952: */
1.178 frystyk 953: HTTRACE(PROT_TRACE, "HTTP........ Looking for `%s\'\n" _
1.141 frystyk 954: HTAnchor_physical(anchor));
955: if ((http = (http_info *) HT_CALLOC(1, sizeof(http_info))) == NULL)
956: HT_OUTOFMEM("HTLoadHTTP");
957: http->net = net;
1.157 frystyk 958: http->request = request;
1.141 frystyk 959: HTNet_setContext(net, http);
960: HTNet_setEventCallback(net, HTTPEvent);
961: HTNet_setEventParam(net, http); /* callbacks get http* */
962:
1.157 frystyk 963: return HTTPEvent(soc, http, HTEvent_BEGIN); /* get it started - ops is ignored */
964: }
965:
966: PRIVATE int FlushPutEvent (HTTimer * timer, void * param, HTEventType type)
967: {
968: http_info * http = (http_info *) param;
969: HTStream * input = HTRequest_inputStream(http->request);
970: HTPostCallback * pcbf = HTRequest_postCallback(http->request);
1.181 frystyk 971: int status = HT_ERROR;
1.157 frystyk 972:
1.165 frystyk 973: http->usedTimer = YES;
1.160 frystyk 974: if (timer != http->timer)
1.178 frystyk 975: HTDEBUGBREAK("HTTP timer %p not in sync\n" _ timer);
976: HTTRACE(PROT_TRACE, "Uploading... Flushing %p with timer %p\n" _ http _ timer);
1.157 frystyk 977:
978: /*
1.181 frystyk 979: ** Call the callback that will provide the data to save
980: ** If the callback returns HT_OK then call it again until
981: ** it returns something else than HT_OK.
1.157 frystyk 982: */
1.181 frystyk 983: if (http && input && pcbf) {
984: status = (*pcbf)(http->request, input);
985: HTTRACE(PROT_TRACE, "Uploading... Callback returned %d\n" _ status);
986: }
1.157 frystyk 987:
988: /*
1.181 frystyk 989: ** If the callback returned something else than HT_OK then delete
990: ** the timer, otherwise update it to a much shorter expiration
991: ** time so that we can write some more data to the net.
1.157 frystyk 992: */
1.181 frystyk 993: if (status != HT_OK) {
994: HTTimer_delete(http->timer);
995: http->timer = NULL;
996: } else if (!http->repetitive_writing) {
997: http->timer = HTTimer_new(NULL, FlushPutEvent, http, HTRepeatWrite, YES, YES);
998: http->repetitive_writing = YES;
999: }
1.157 frystyk 1000: return HT_OK;
1.141 frystyk 1001: }
1002:
1003: PRIVATE int HTTPEvent (SOCKET soc, void * pVoid, HTEventType type)
1004: {
1005: http_info * http = (http_info *)pVoid;
1006: int status = HT_ERROR;
1007: HTNet * net = http->net;
1008: HTRequest * request = HTNet_request(net);
1.144 frystyk 1009: HTParentAnchor * anchor = HTRequest_anchor(request);
1010: HTHost * host = HTNet_host(net);
1.141 frystyk 1011:
1012: /*
1.166 frystyk 1013: ** Check whether we have been interrupted or timed out
1.141 frystyk 1014: */
1015: if (type == HTEvent_BEGIN) {
1.134 frystyk 1016: http->next = HTTP_OK;
1017: http->result = HT_ERROR;
1.141 frystyk 1018: } else if (type == HTEvent_CLOSE) {
1.179 frystyk 1019: long read_len = HTNet_bytesRead(net);
1020: long doc_len = HTAnchor_length(anchor);
1021:
1022: /*
1023: ** It is OK to get a close if a) we don't pipeline and b)
1.182 ! frystyk 1024: ** we have the expected amount of data, and c) we haven't
! 1025: ** recieved a 100 Continue code. In case we don't
1.179 frystyk 1026: ** know how much data to expect, we must accept it asis.
1027: */
1028: if (HTHost_numberOfOutstandingNetObjects(host) == 1 &&
1.182 ! frystyk 1029: http->result != HT_CONTINUE && (doc_len<0 || doc_len==read_len)) {
1.179 frystyk 1030: HTTPCleanup(request, HT_LOADED);
1031: } else {
1032: HTRequest_addError(request, ERR_FATAL, NO, HTERR_INTERRUPTED,
1033: NULL, 0, "HTLoadHTTP");
1034: HTTPCleanup(request, HT_INTERRUPTED);
1035: }
1.166 frystyk 1036: return HT_OK;
1037: } else if (type == HTEvent_TIMEOUT) {
1038: HTRequest_addError(request, ERR_FATAL, NO, HTERR_TIME_OUT,
1039: NULL, 0, "HTLoadHTTP");
1040: HTTPCleanup(request, HT_TIMEOUT);
1.94 frystyk 1041: return HT_OK;
1.141 frystyk 1042: } else if (type == HTEvent_END) {
1043: HTTPCleanup(request, http->result);
1044: return HT_OK;
1045: } else if (type == HTEvent_RESET) {
1046: HTTPCleanup(request, HT_RECOVER_PIPE);
1.145 frystyk 1047: http->state = HTTP_BEGIN;
1.141 frystyk 1048: return HT_OK;
1049: }
1050:
1.71 frystyk 1051: /* Now jump into the machine. We know the state from the previous run */
1052: while (1) {
1053: switch (http->state) {
1.134 frystyk 1054: case HTTP_BEGIN:
1.144 frystyk 1055: status = HTHost_connect(host, net, HTAnchor_physical(anchor), HTTP_PORT);
1056: host = HTNet_host(net);
1.153 frystyk 1057: if (status == HT_OK) {
1.140 frystyk 1058:
1.123 frystyk 1059: /*
1.140 frystyk 1060: ** Check the protocol class to see if we have connected to a
1061: ** the right class of server, in this case HTTP. If we don't
1062: ** know the server then assume a HTTP/1.0
1.123 frystyk 1063: */
1064: {
1065: char * s_class = HTHost_class(host);
1.140 frystyk 1066: if (!s_class) {
1.174 frystyk 1067: if (HTRequest_proxy(request) == NULL) {
1068: HTAssocList * alist = HTRequest_connection(request);
1069: if (!(alist && HTAssocList_findObject(alist, "close")))
1070: HTRequest_addConnection(request, "Keep-Alive", "");
1071: }
1.140 frystyk 1072: HTHost_setClass(host, "http");
1073: } else if (strcasecomp(s_class, "http")) {
1.123 frystyk 1074: HTRequest_addError(request, ERR_FATAL, NO, HTERR_CLASS,
1075: NULL, 0, "HTLoadHTTP");
1076: http->state = HTTP_ERROR;
1077: break;
1078: }
1.95 frystyk 1079: }
1.147 frystyk 1080:
1.156 frystyk 1081: if (ConnectionMode & HTTP_11_NO_PIPELINING) {
1.178 frystyk 1082: HTTRACE(PROT_TRACE, "HTTP........ Mode is HTTP/1.1 WITH NO PIPELINING\n");
1.147 frystyk 1083: HTRequest_setFlush(request, YES);
1084: } else if (ConnectionMode & HTTP_FORCE_10) {
1.178 frystyk 1085: HTTRACE(PROT_TRACE, "HTTP........ Mode is FORCE HTTP/1.0\n");
1.147 frystyk 1086: HTHost_setVersion(host, HTTP_10);
1087: }
1088:
1.151 frystyk 1089: if (HTNet_preemptive(net)) {
1.178 frystyk 1090: HTTRACE(PROT_TRACE, "HTTP........ Force flush on preemptive load\n");
1.151 frystyk 1091: HTRequest_setFlush(request, YES);
1092: }
1093:
1.147 frystyk 1094: /* Jump to next state */
1.144 frystyk 1095: http->state = HTTP_NEED_STREAM;
1.159 frystyk 1096: } else if (status == HT_WOULD_BLOCK || status == HT_PENDING) {
1.144 frystyk 1097: return HT_OK;
1.171 frystyk 1098: } else if (status == HT_NO_HOST) {
1099: http->result = HT_NO_HOST;
1100: http->state = HTTP_ERROR;
1.159 frystyk 1101: } else
1.144 frystyk 1102: http->state = HTTP_ERROR; /* Error or interrupt */
1103: break;
1104:
1105: case HTTP_NEED_STREAM:
1.138 frystyk 1106:
1.144 frystyk 1107: /*
1108: ** Create the stream pipe FROM the channel to the application.
1109: ** The target for the input stream pipe is set up using the
1110: ** stream stack.
1111: */
1.167 frystyk 1112: {
1113: /*
1114: ** during a recovery, we might keep the same HTNet object.
1115: ** if so, reuse it's read stream
1116: */
1.176 frystyk 1117: HTStream * me = HTNet_readStream( net );
1.167 frystyk 1118: if ( me == NULL ) {
1.173 frystyk 1119: me = HTStreamStack(WWW_HTTP,
1120: HTRequest_outputFormat(request),
1121: HTRequest_outputStream(request),
1122: request, YES);
1.178 frystyk 1123: #ifdef HTDEBUG
1.173 frystyk 1124: if (PROT_TRACE) {
1125: if (!htfp) htfp = fopen(HTTP_OUTPUT, "ab");
1126: if (htfp) {
1127: me = HTTee(me, HTFWriter_new(request, htfp, YES), NULL);
1.178 frystyk 1128: HTTRACE(PROT_TRACE, "HTTP........ Dumping response to `%s\'\n" _ HTTP_OUTPUT);
1.173 frystyk 1129: }
1130: }
1.178 frystyk 1131: #endif /* HTDEBUG */
1.173 frystyk 1132:
1133: HTNet_setReadStream(net, me);
1.167 frystyk 1134: }
1135: HTRequest_setOutputConnected(request, YES);
1.144 frystyk 1136: }
1.127 frystyk 1137:
1.144 frystyk 1138: /*
1139: ** Create the stream pipe TO the channel from the application
1140: ** and hook it up to the request object
1141: */
1142: {
1143: HTChannel * channel = HTHost_channel(host);
1144: HTOutputStream * output = HTChannel_getChannelOStream(channel);
1145: int version = HTHost_version(host);
1146: HTStream * app = NULL;
1147:
1.178 frystyk 1148: #ifdef HTDEBUG
1.144 frystyk 1149: if (PROT_TRACE) {
1.162 frystyk 1150: if (!htfp) htfp = fopen(HTTP_OUTPUT, "ab");
1151: if (htfp) {
1.144 frystyk 1152: output = (HTOutputStream *)
1.162 frystyk 1153: HTTee((HTStream *) output, HTFWriter_new(request, htfp, YES), NULL);
1.178 frystyk 1154: HTTRACE(PROT_TRACE, "HTTP........ Dumping request to `%s\'\n" _ HTTP_OUTPUT);
1.144 frystyk 1155: }
1156: }
1.178 frystyk 1157: #endif /* HTDEBUG */
1.144 frystyk 1158: app = HTMethod_hasEntity(HTRequest_method(request)) ?
1159: HTMIMERequest_new(request,
1160: HTTPRequest_new(request, (HTStream *) output, NO,
1161: version),
1162: YES) :
1163: HTTPRequest_new(request, (HTStream *) output, YES, version);
1164: HTRequest_setInputStream(request, app);
1165: }
1.88 frystyk 1166:
1.144 frystyk 1167: /*
1168: ** Set up concurrent read/write if this request isn't the
1169: ** source for a PUT or POST. As source we don't start reading
1170: ** before all destinations are ready. If destination then
1171: ** register the input stream and get ready for read
1172: */
1173: if (HTRequest_isDestination(request)) {
1174: HTHost_register(host, net, HTEvent_READ);
1175: HTRequest_linkDestination(request);
1176: }
1177: http->state = HTTP_CONNECTED;
1178: type = HTEvent_WRITE; /* fresh, so try a write */
1.71 frystyk 1179: break;
1180:
1.87 frystyk 1181: /* As we can do simultanous read and write this is now one state */
1.141 frystyk 1182: case HTTP_CONNECTED:
1183: if (type == HTEvent_WRITE) {
1.157 frystyk 1184: HTStream * input = HTRequest_inputStream(request);
1185: HTPostCallback * pcbf = HTRequest_postCallback(request);
1186: status = HTRequest_flush(request) ?
1187: HTHost_forceFlush(host) : (*input->isa->flush)(input);
1188:
1189: /*
1190: ** Check to see if we are uploading something or just a normal
1191: ** GET kind of thing.
1192: */
1193: if (pcbf) {
1194: if (http->lock == NO) {
1195: int retrys = HTRequest_retrys(request);
1.169 frystyk 1196: ms_t delay = retrys > 3 ? HTSecondWriteDelay : HTFirstWriteDelay;
1.165 frystyk 1197: if (!http->timer && !http->usedTimer) {
1.162 frystyk 1198: http->timer = HTTimer_new(NULL, FlushPutEvent,
1199: http, delay, YES, NO);
1.178 frystyk 1200: HTTRACE(PROT_TRACE, "Uploading... Holding %p for %lu ms using time %p\n" _
1201: http _ delay _ http->timer);
1.157 frystyk 1202: HTHost_register(host, net, HTEvent_READ);
1203: }
1204: http->lock = YES;
1205: }
1206: type = HTEvent_READ;
1207: } else {
1208:
1209: /*
1210: ** Check to see if we can start a new request
1211: ** pending in the host object.
1212: */
1213: HTHost_launchPending(host);
1214: type = HTEvent_READ;
1215: }
1216:
1217: /* Now check the status code */
1218: if (status == HT_WOULD_BLOCK)
1219: return HT_OK;
1220: else if (status == HT_PAUSE || status == HT_LOADED) {
1221: type = HTEvent_READ;
1.168 frystyk 1222: } else if (status==HT_ERROR)
1.157 frystyk 1223: http->state = HTTP_RECOVER_PIPE;
1224: } else if (type == HTEvent_FLUSH) {
1225: HTStream * input = HTRequest_inputStream(request);
1226: if (input == NULL)
1227: return HT_ERROR;
1228: return (*input->isa->flush)(input);
1229: } else if (type == HTEvent_READ) {
1230: status = HTHost_read(host, net);
1231: if (status == HT_WOULD_BLOCK)
1232: return HT_OK;
1233: else if (status == HT_CONTINUE) {
1.178 frystyk 1234: HTTRACE(PROT_TRACE, "HTTP........ Continuing\n");
1.157 frystyk 1235: http->lock = NO;
1236: continue;
1237: } else if (status==HT_LOADED)
1238: http->state = http->next; /* Jump to next state (OK or ERROR) */
1239: else if (status==HT_CLOSED)
1240: http->state = HTTP_RECOVER_PIPE;
1.167 frystyk 1241: else if (status == HT_ERROR)
1242: http->state = HTTP_KILL_PIPE;
1.157 frystyk 1243: else
1244: http->state = HTTP_ERROR;
1245: } else {
1246: http->state = HTTP_ERROR; /* don't know how to handle OOB */
1247: }
1248: break;
1.141 frystyk 1249:
1.134 frystyk 1250: case HTTP_OK:
1251: HTTPCleanup(request, http->result);
1.94 frystyk 1252: return HT_OK;
1.71 frystyk 1253: break;
1.141 frystyk 1254:
1255: case HTTP_RECOVER_PIPE:
1256: {
1257: /*
1258: ** If this is a persistent connection and we get a close
1259: ** then it is an error and we should recover from it by
1260: ** restarting the pipe line of requests if any
1261: */
1.151 frystyk 1262: if (HTHost_isPersistent(host) && !HTHost_closeNotification(host)) {
1.143 frystyk 1263: if (host == NULL) return HT_ERROR;
1.144 frystyk 1264: HTRequest_setFlush(request, YES);
1.182 ! frystyk 1265:
! 1266: /*
! 1267: ** If we already have recovered more than we want and
! 1268: ** this call returns NO then simply kill the pipe.
! 1269: ** Otherwise we may loop forever.
! 1270: */
! 1271: if (HTHost_recoverPipe(host) != YES) {
! 1272: HTRequest_addError(request, ERR_FATAL, NO, HTERR_BAD_REPLY,
! 1273: NULL, 0, "HTTPEvent");
! 1274: http->state = HTTP_KILL_PIPE;
! 1275: break;
! 1276: }
1.144 frystyk 1277: return HT_OK;
1.141 frystyk 1278: } else
1279: http->state = HTTP_OK;
1280: }
1.143 frystyk 1281: break;
1.141 frystyk 1282:
1.167 frystyk 1283: case HTTP_KILL_PIPE:
1284: if (host == NULL) return HT_ERROR;
1285: HTHost_killPipe(host);
1286: return HT_OK;
1287: break;
1288:
1.71 frystyk 1289: case HTTP_ERROR:
1.143 frystyk 1290: HTTPCleanup(request, http->result);
1291: return HT_OK;
1292: break;
1293:
1.141 frystyk 1294: default:
1.178 frystyk 1295: HTDEBUGBREAK("Bad http state %d\n" _ http->state);
1.71 frystyk 1296: }
1297: } /* End of while(1) */
1298: }
1.88 frystyk 1299:
1.147 frystyk 1300: PUBLIC void HTTP_setConnectionMode (HTTPConnectionMode mode)
1301: {
1302: ConnectionMode = mode;
1303: }
1304:
1305: PUBLIC HTTPConnectionMode HTTP_connectionMode (void)
1306: {
1307: return ConnectionMode;
1308: }
1.21 luotonen 1309:
1.169 frystyk 1310: PUBLIC BOOL HTTP_setBodyWriteDelay (ms_t first_try, ms_t second_try)
1311: {
1312: if (first_try > 20 && second_try >= first_try) {
1313: HTFirstWriteDelay = first_try;
1314: HTSecondWriteDelay = second_try;
1315: return YES;
1316: }
1317: return NO;
1318: }
1319:
1320: PUBLIC void HTTP_bodyWriteDelay (ms_t * first_try, ms_t * second_try)
1321: {
1322: *first_try = HTFirstWriteDelay;
1323: *second_try = HTSecondWriteDelay;
1.170 frystyk 1324: }
1325:
Webmaster