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