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