Annotation of libwww/Library/src/HTTPReq.c, revision 2.7
2.1 frystyk 1: /* HTTPReq.c
2: ** MULTITHREADED IMPLEMENTATION OF HTTP CLIENT
3: **
4: ** This module implements the output stream for HTTP used for sending
5: ** requests with or without a entity body.
6: **
7: ** History:
8: ** Jan 95 HFN Written from scratch
9: **
10: */
11:
12: /* Library Includes */
13: #include "tcp.h"
14: #include "HTUtils.h"
15: #include "HTString.h"
16: #include "HTParse.h"
2.4 frystyk 17: #include "HTFormat.h"
2.1 frystyk 18: #include "HTThread.h"
19: #include "HTTCP.h"
20: #include "HTWriter.h"
21: #include "HTChunk.h"
22: #include "HTTPReq.h" /* Implements */
23:
24: /* Type definitions and global variables etc. local to this module */
25: extern char * HTAppName; /* Application name: please supply */
26: extern char * HTAppVersion; /* Application version: please supply */
27: PUBLIC char * HTProxyHeaders = NULL; /* Headers to pass as-is */
28:
29: /* Macros and other defines */
30: #define HTTP_VERSION "HTTP/1.0"
31: #define MIME_VERSION "MIME/1.0"
32: #define PUTC(c) (*me->target->isa->put_character)(me->target, c)
33: #define PUTS(s) (*me->target->isa->put_string)(me->target, s)
34: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
2.7 ! frystyk 35: #define FREE_TARGET
2.1 frystyk 36: #define ABORT_TARGET (*me->target->isa->abort)(me->target, e)
37:
38: /* Type definitions and global variables etc. local to this module */
39:
40: PRIVATE char linebuf[256]; /* @@@ */
41:
42: struct _HTStream {
43: CONST HTStreamClass * isa;
44: HTStream * target;
45: HTRequest * request;
2.7 ! frystyk 46: SOCKFD sockfd;
2.1 frystyk 47: HTChunk * buffer;
48: BOOL transparent;
49: };
50:
51: /* ------------------------------------------------------------------------- */
52: /* HTTP Output Request Stream */
53: /* ------------------------------------------------------------------------- */
54:
55: /* HTTPMakeRequest
56: **
57: ** This function composes the HTTP request header.
58: */
59: PRIVATE void HTTPMakeRequest ARGS2(HTStream *, me, HTRequest *, request)
60: {
61: HTChunk *header = me->buffer;
62: HTParentAnchor *entity =
2.7 ! frystyk 63: (request->source && request->source->anchor) ?
! 64: request->source->anchor : request->anchor;
2.1 frystyk 65:
66: /* Generate the HTTP/1.0 RequestLine */
67: if (request->method != METHOD_INVALID) {
68: HTChunkPuts(header, HTMethod_name(request->method));
69: HTChunkPutc(header, ' ');
70: } else
71: HTChunkPuts(header, "GET ");
72:
73: /* If we are using a proxy then only take the `path' info in the URL */
74: {
2.6 frystyk 75: char *addr = HTAnchor_physical(request->anchor);
76: char *fullurl = HTParse(addr, "", PARSE_PATH|PARSE_PUNCTUATION);
2.1 frystyk 77: if (request->using_proxy) {
78: HTChunkPuts(header, fullurl+1);
79: } else {
80: HTChunkPuts(header, fullurl);
81: }
82: free(fullurl);
83: }
84: HTChunkPutc(header, ' ');
85: HTChunkPuts(header, HTTP_VERSION);
86: HTChunkPutc(header, CR);
87: HTChunkPutc(header, LF);
88:
2.4 frystyk 89: /* General Headers */
90: if (request->GenMask & HT_DATE) {
2.1 frystyk 91: time_t local = time(NULL);
92: sprintf(linebuf, "Date: %s%c%c", HTDateTimeStr(&local, NO), CR,LF);
93: HTChunkPuts(header, linebuf);
94: }
2.4 frystyk 95: if (request->GenMask & HT_FORWARDED) { /* @@@@@@ */
96: }
97: if (request->GenMask & HT_MESSAGE_ID) {
2.1 frystyk 98: CONST char *msgid = HTMessageIdStr();
99: if (msgid) {
100: sprintf(linebuf, "Message-ID: %s%c%c", msgid, CR, LF);
101: HTChunkPuts(header, linebuf);
102: }
103: }
2.4 frystyk 104: if (request->GenMask & HT_MIME) {
2.1 frystyk 105: sprintf(linebuf, "MIME-Version: %s%c%c", MIME_VERSION, CR, LF);
106: HTChunkPuts(header, linebuf);
107: }
108:
2.4 frystyk 109: /* Request Headers */
110: if (request->RequestMask & HT_ACCEPT_TYPE) {
2.1 frystyk 111: int list;
112: HTList *cur;
113: for (list=0; list<2; list++) {
114: if ((!list && ((cur=HTConversions) != NULL)) ||
115: (list && ((cur=request->conversions) != NULL))) {
2.4 frystyk 116: HTPresentation *pres;
2.1 frystyk 117: while ((pres =(HTPresentation *) HTList_nextObject(cur))) {
118: if (pres->rep_out == WWW_PRESENT) {
119: if (pres->quality != 1.0) {
120: sprintf(linebuf, "Accept: %s; q=%1.1f%c%c",
121: HTAtom_name(pres->rep),
122: pres->quality, CR, LF);
123: } else {
124: sprintf(linebuf, "Accept: %s%c%c",
125: HTAtom_name(pres->rep), CR, LF);
126: }
127: HTChunkPuts(header, linebuf);
128: }
129: }
130: }
131: }
132: }
2.4 frystyk 133: if (request->RequestMask & HT_ACCEPT_CHAR) {
134: BOOL first=YES;
135: int list;
136: HTList *cur;
137: for (list=0; list<2; list++) {
138: if ((!list && ((cur=HTCharsets) != NULL)) ||
139: (list && ((cur=request->charsets) != NULL))) {
140: HTAcceptNode *pres;
141: while ((pres = (HTAcceptNode *) HTList_nextObject(cur))) {
2.5 frystyk 142: if (first) {
143: HTChunkPuts(header, "Accept-Charset: ");
144: first=NO;
145: }
2.4 frystyk 146: if (cur->next)
147: sprintf(linebuf, "%s,", HTAtom_name(pres->atom));
148: else
149: sprintf(linebuf, "%s%c%c", HTAtom_name(pres->atom),
150: CR, LF);
151: HTChunkPuts(header, linebuf);
152: }
153: }
154: }
155: }
156: if (request->RequestMask & HT_ACCEPT_ENC) {
157: BOOL first=YES;
158: int list;
159: HTList *cur;
160: for (list=0; list<2; list++) {
161: if ((!list && ((cur=HTEncodings) != NULL)) ||
162: (list && ((cur=request->encodings) != NULL))) {
163: HTAcceptNode *pres;
164: while ((pres = (HTAcceptNode *) HTList_nextObject(cur))) {
2.5 frystyk 165: if (first) {
166: HTChunkPuts(header, "Accept-Encoding: ");
167: first=NO;
168: }
2.4 frystyk 169: if (cur->next)
170: sprintf(linebuf, "%s,", HTAtom_name(pres->atom));
171: else
172: sprintf(linebuf, "%s%c%c", HTAtom_name(pres->atom),
173: CR, LF);
174: HTChunkPuts(header, linebuf);
175: }
176: }
177: }
178: }
179: if (request->RequestMask & HT_ACCEPT_LAN) {
180: BOOL first=YES;
181: int list;
182: HTList *cur;
183: for (list=0; list<2; list++) {
184: if ((!list && ((cur=HTLanguages) != NULL)) ||
185: (list && ((cur=request->languages) != NULL))) {
186: HTAcceptNode *pres;
187: while ((pres = (HTAcceptNode *) HTList_nextObject(cur))) {
2.5 frystyk 188: if (first) {
189: HTChunkPuts(header, "Accept-Language: ");
190: first=NO;
191: }
2.4 frystyk 192: if (cur->next)
193: sprintf(linebuf, "%s,", HTAtom_name(pres->atom));
194: else
195: sprintf(linebuf, "%s%c%c", HTAtom_name(pres->atom),
196: CR, LF);
197: HTChunkPuts(header, linebuf);
198: }
199: }
200: }
201: }
202: if (request->authorization != NULL) { /* Put out authorization */
2.1 frystyk 203: sprintf(linebuf, "Authorization: %s%c%c", request->authorization,
204: CR, LF);
205: HTChunkPuts(header, linebuf);
206: }
2.4 frystyk 207: if (request->RequestMask & HT_FROM) {
2.1 frystyk 208: CONST char *mailaddress = HTGetMailAddress();
209: if (mailaddress) {
210: sprintf(linebuf, "From: %s%c%c", mailaddress, CR, LF);
211: HTChunkPuts(header, linebuf);
212: }
213: }
2.4 frystyk 214: if (request->RequestMask & HT_PRAGMA) {
2.1 frystyk 215: sprintf(linebuf, "Pragma: %s%c%c", "no-cache", CR, LF);
216: HTChunkPuts(header, linebuf);
217: }
2.4 frystyk 218: if (request->RequestMask & HT_REFERER && request->parentAnchor) {
2.1 frystyk 219: char *act = HTAnchor_address((HTAnchor *) request->anchor);
220: char *parent = HTAnchor_address((HTAnchor *) request->parentAnchor);
221: char *relative = HTParse(parent, act,
222: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
223: if (relative && *relative) {
224: sprintf(linebuf, "Referer: %s%c%c", parent, CR, LF);
225: HTChunkPuts(header, linebuf);
226: }
227: free(act);
228: free(parent);
229: free(relative);
230: }
2.4 frystyk 231: if (request->RequestMask & HT_USER_AGENT) {
2.1 frystyk 232: sprintf(linebuf, "User-Agent: %s/%s libwww/%s%c%c",
233: HTAppName ? HTAppName : "unknown",
234: HTAppVersion ? HTAppVersion : "0.0",
235: HTLibraryVersion, CR, LF);
236: HTChunkPuts(header, linebuf);
237: }
238:
239: /* Now put out entity headers if we are using PUT or POST. If we have a
240: ** PostAnchor then we take the information from this and uses the
241: ** destination anchor to contain the reply. Otherwise, we have created an
242: ** anchor (using internal editing etc) and we can use the destination
243: ** anchor directly.
244: */
245: if (request->method==METHOD_PUT || request->method==METHOD_POST) {
2.2 frystyk 246: if (request->EntityMask & HT_ALLOW) { /* @@@@@@@@@@ */
2.1 frystyk 247:
248: }
249: if (request->EntityMask & HT_CONTENT_ENCODING &&
250: entity->content_encoding) {
251: sprintf(linebuf, "Content-Encoding: %s%c%c",
252: HTAtom_name(entity->content_encoding), CR, LF);
253: HTChunkPuts(header, linebuf);
254: }
2.6 frystyk 255:
256: /* @@@ SHOULD BE A LIST @@@ */
257: if (request->EntityMask & HT_CONTENT_LANGUAGE &&
258: entity->content_language) {
2.3 frystyk 259: sprintf(linebuf, "Content-Language: %s%c%c",
260: HTAtom_name(entity->content_language), CR, LF);
261: HTChunkPuts(header, linebuf);
2.1 frystyk 262: }
263: if (request->EntityMask & HT_CONTENT_LENGTH) { /* Must be there!!! */
264: sprintf(linebuf, "Content-Length: %ld%c%c",
265: entity->content_length, CR, LF);
266: HTChunkPuts(header, linebuf);
267: }
268: if (request->EntityMask & HT_CTE && entity->cte) {
269: sprintf(linebuf, "Content-Transfer-Encoding: %s%c%c",
270: HTAtom_name(entity->cte), CR, LF);
271: HTChunkPuts(header, linebuf);
272: }
273: if (request->EntityMask & HT_CONTENT_TYPE && entity->content_type) {
2.2 frystyk 274: sprintf(linebuf, "Content-Type: %s",
275: HTAtom_name(entity->content_type));
276: if (entity->charset) {
277: strcat(linebuf, "; charset=");
278: strcat(linebuf, HTAtom_name(entity->charset));
279: }
280: if (entity->level) {
281: strcat(linebuf, "; level=");
282: strcat(linebuf, HTAtom_name(entity->level));
283: }
2.1 frystyk 284: HTChunkPuts(header, linebuf);
2.2 frystyk 285: HTChunkPutc(header, CR);
286: HTChunkPutc(header, LF);
2.1 frystyk 287: }
288: if (request->EntityMask & HT_DERIVED_FROM && entity->derived_from) {
289: sprintf(linebuf, "Derived-From: %s%c%c", entity->derived_from,
290: CR, LF);
291: HTChunkPuts(header, linebuf);
292: }
2.2 frystyk 293: if (request->EntityMask & HT_EXPIRES) { /* @@@@@@@@@@ */
2.1 frystyk 294:
295: }
2.2 frystyk 296: if (request->EntityMask & HT_LAST_MODIFIED) { /* @@@@@@@@@@ */
2.1 frystyk 297:
298: }
2.2 frystyk 299: if (request->EntityMask & HT_LINK) { /* @@@@@@@@@@ */
2.1 frystyk 300:
301: }
2.2 frystyk 302: if (request->EntityMask & HT_TITLE) { /* @@@@@@@@@@ */
2.1 frystyk 303:
304: }
2.2 frystyk 305: if (request->EntityMask & HT_URI) { /* @@@@@@@@@@ */
2.1 frystyk 306:
307: }
308: if (request->EntityMask & HT_VERSION && entity->version) {
309: sprintf(linebuf, "Version: %s%c%c", entity->version, CR, LF);
310: HTChunkPuts(header, linebuf);
311: }
312: }
313:
314: /* Put out extra information if any */
315: if (request->ExtraHeaders)
316: HTChunkPuts(header, request->ExtraHeaders);
317:
318: HTChunkPutc(header, CR); /* Blank line means "end" */
319: HTChunkPutc(header, LF);
320: HTChunkTerminate(header);
321: if (PROT_TRACE)
322: fprintf(TDEST, "HTTP Tx..... %s", header->data);
323: }
324:
325: PRIVATE int HTTPRequest_put_character ARGS2(HTStream *, me, char, c)
326: {
2.7 ! frystyk 327: if (!me->target) {
2.1 frystyk 328: return HT_WOULD_BLOCK;
2.7 ! frystyk 329: } else if (me->transparent)
2.1 frystyk 330: return PUTC(c);
331: else {
332: int status;
333: HTTPMakeRequest(me, me->request); /* Generate header */
334: if ((status=PUTBLOCK(me->buffer->data, me->buffer->size-1)) == HT_OK) {
335: me->transparent = YES;
336: return PUTC(c);
337: }
338: return status;
339: }
340: }
341:
342: PRIVATE int HTTPRequest_put_string ARGS2(HTStream *, me, CONST char*, s)
343: {
2.7 ! frystyk 344: if (!me->target) {
2.1 frystyk 345: return HT_WOULD_BLOCK;
2.7 ! frystyk 346: } else if (me->transparent)
2.1 frystyk 347: return PUTS(s);
348: else {
349: int status;
350: HTTPMakeRequest(me, me->request); /* Generate header */
351: if ((status=PUTBLOCK(me->buffer->data, me->buffer->size-1)) == HT_OK) {
352: me->transparent = YES;
353: return PUTS(s);
354: }
355: return status;
356: }
357: }
358:
359: PRIVATE int HTTPRequest_put_block ARGS3(HTStream *, me, CONST char*, b, int, l)
360: {
2.7 ! frystyk 361: if (!me->target) {
2.1 frystyk 362: return HT_WOULD_BLOCK;
2.7 ! frystyk 363: } else if (me->transparent)
2.1 frystyk 364: return PUTBLOCK(b, l);
365: else {
366: int status;
367: HTTPMakeRequest(me, me->request); /* Generate header */
368: if ((status=PUTBLOCK(me->buffer->data, me->buffer->size-1)) == HT_OK) {
369: me->transparent = YES;
370: return PUTBLOCK(b, l);
371: }
372: return status;
373: }
374: }
375:
376: /*
377: ** Flushes data but doesn't free stream object
378: */
379: PRIVATE int HTTPRequest_flush ARGS1(HTStream *, me)
380: {
2.7 ! frystyk 381: if (!me->target) {
2.1 frystyk 382: return HT_WOULD_BLOCK;
2.7 ! frystyk 383: } else if (!me->transparent) {
2.1 frystyk 384: int status;
385: HTTPMakeRequest(me, me->request); /* Generate header */
386: if ((status=PUTBLOCK(me->buffer->data, me->buffer->size-1)) == HT_OK)
387: me->transparent = YES;
388: else
389: return status;
390: }
391: return HT_OK;
392: }
393:
394: /*
395: ** Flushes data and frees stream object
396: */
397: PRIVATE int HTTPRequest_free ARGS1(HTStream *, me)
398: {
2.7 ! frystyk 399: int status;
! 400: if (!me->target) {
2.1 frystyk 401: return HT_WOULD_BLOCK;
2.7 ! frystyk 402: } else if (!me->transparent) {
2.1 frystyk 403: HTTPMakeRequest(me, me->request); /* Generate header */
2.6 frystyk 404: if ((status = PUTBLOCK(me->buffer->data, me->buffer->size-1)) == HT_OK)
2.1 frystyk 405: me->transparent = YES;
406: else
407: return status;
408: }
2.7 ! frystyk 409: if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
2.6 frystyk 410: return HT_WOULD_BLOCK;
2.1 frystyk 411: HTChunkFree(me->buffer);
412: free(me);
2.7 ! frystyk 413: return status;
2.1 frystyk 414: }
415:
416: PRIVATE int HTTPRequest_abort ARGS2(HTStream *, me, HTError, e)
417: {
418: if (me->target)
419: ABORT_TARGET;
420: HTChunkFree(me->buffer);
421: free(me);
422: if (PROT_TRACE)
423: fprintf(TDEST, "HTTPRequest. ABORTING...\n");
424: return HT_ERROR;
425: }
426:
427: /* HTTPRequest Stream
428: ** -----------------
429: */
430: PRIVATE CONST HTStreamClass HTTPRequestClass =
431: {
432: "HTTPRequest",
433: HTTPRequest_flush,
434: HTTPRequest_free,
435: HTTPRequest_abort,
436: HTTPRequest_put_character,
437: HTTPRequest_put_string,
438: HTTPRequest_put_block
439: };
440:
441: PUBLIC HTStream * HTTPRequest_new ARGS2(HTRequest *, request,
442: HTStream *, target)
443: {
444: HTStream * me = (HTStream *) calloc(1, sizeof(HTStream));
445: if (!me) outofmem(__FILE__, "HTTPRequest_new");
446: me->isa = &HTTPRequestClass;
447: me->target = target;
448: me->request = request;
449: me->buffer = HTChunkCreate(512);
450: me->transparent = NO;
451: return me;
452: }
453:
454:
Webmaster