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