Annotation of libwww/Library/src/HTMIMERq.c, revision 2.35
2.1 frystyk 1: /* HTMIMERq.c
2.10 frystyk 2: ** MIME ENTITY HEADERS GENERATION
2.1 frystyk 3: **
2.15 frystyk 4: ** (c) COPYRIGHT MIT 1995.
5: ** Please first read the full copyright statement in the file COPYRIGH.
2.35 ! frystyk 6: ** @(#) $Id: HTMIMERq.c,v 2.34 1998/03/05 21:56:19 frystyk Exp $
2.15 frystyk 7: **
2.1 frystyk 8: ** This module implements the output stream for MIME used for sending
2.10 frystyk 9: ** requests with a entity body to HTTP, NEWS, etc. or for generating
10: ** responses
2.1 frystyk 11: **
12: ** History:
13: ** Jan 95 HFN Written
14: */
15:
16: /* Library Includes */
2.14 frystyk 17: #include "sysdep.h"
2.21 frystyk 18: #include "WWWUtil.h"
19: #include "WWWCore.h"
2.7 frystyk 20: #include "HTAncMan.h"
2.1 frystyk 21: #include "HTNetMan.h"
2.21 frystyk 22: #include "HTReqMan.h"
2.2 frystyk 23: #include "HTHeader.h"
2.21 frystyk 24: #include "HTMIMERq.h" /* Implements */
2.1 frystyk 25:
2.17 frystyk 26: #define PUTC(c) (*me->target->isa->put_character)(me->target, c)
27: #define PUTS(s) (*me->target->isa->put_string)(me->target, s)
2.1 frystyk 28: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
29:
30: struct _HTStream {
2.14 frystyk 31: const HTStreamClass * isa;
2.1 frystyk 32: HTStream * target;
33: HTRequest * request;
2.10 frystyk 34: BOOL endHeader;
2.1 frystyk 35: BOOL transparent;
36: };
37:
2.27 frystyk 38: #define HT_MAX_WAIT 8 /* Max number of secs to wait for PUT */
2.25 frystyk 39:
2.32 frystyk 40: PRIVATE int MIMERequest_put_block (HTStream * me, const char * b, int l);
41:
2.1 frystyk 42: /* ------------------------------------------------------------------------- */
43: /* MIME Output Request Stream */
44: /* ------------------------------------------------------------------------- */
45:
46: /* MIMEMakeRequest
47: ** ---------------
2.10 frystyk 48: ** Generates the BODY parts of a MIME message.
2.1 frystyk 49: */
2.2 frystyk 50: PRIVATE int MIMEMakeRequest (HTStream * me, HTRequest * request)
2.1 frystyk 51: {
2.17 frystyk 52: char crlf[3];
2.10 frystyk 53: char linebuf[256]; /* @@@ */
2.21 frystyk 54: HTParentAnchor * entity = HTRequest_entityAnchor(request);
55: HTEnHd EntityMask = HTRequest_enHd(request);
2.34 frystyk 56: BOOL transfer_coding = NO; /* We should get this from the Host object */
2.17 frystyk 57: *crlf = CR; *(crlf+1) = LF; *(crlf+2) = '\0';
2.1 frystyk 58:
2.21 frystyk 59: if (EntityMask & HT_E_ALLOW) {
2.34 frystyk 60: BOOL first = YES;
61: int cnt;
62: HTMethod methodset = HTAnchor_allow(entity);
63: for (cnt=0; cnt<sizeof(HTMethod)<<3 ; cnt++) {
64: if (methodset & (1<<cnt)) {
65: if (first) {
66: PUTS("Allow: ");
67: first = NO;
68: } else
69: PUTC(',');
70: PUTS(HTMethod_name(1<<cnt));
71: }
72: }
73: if (!first) PUTBLOCK(crlf, 2);
2.10 frystyk 74: }
2.35 ! frystyk 75: if (EntityMask & HT_E_CONTENT_ENCODING && entity->content_encoding) {
2.17 frystyk 76: BOOL first = YES;
77: HTList * cur = entity->content_encoding;
78: HTEncoding pres;
2.35 ! frystyk 79: while ((pres = (HTEncoding) HTList_nextObject(cur)) &&
! 80: !HTFormat_isUnityContent(pres)) {
2.17 frystyk 81: if (first) {
82: PUTS("Content-Encoding: ");
83: first = NO;
84: } else
85: PUTC(',');
86: PUTS(HTAtom_name(pres));
87: }
88: if (!first) PUTBLOCK(crlf, 2);
2.1 frystyk 89: }
2.34 frystyk 90: if (EntityMask & HT_E_CTE && entity->cte) {
91: HTEncoding cte = HTAnchor_contentTransferEncoding(entity);
92: if (!HTFormat_isUnityTransfer(cte)) {
93: sprintf(linebuf, "Content-Transfer-Encoding: %s%c%c",
94: HTAtom_name(cte), CR, LF);
95: PUTBLOCK(linebuf, (int) strlen(linebuf));
96: }
97: }
2.21 frystyk 98: if (EntityMask & HT_E_CONTENT_LANGUAGE && entity->content_language){
2.17 frystyk 99: BOOL first = YES;
100: HTList * cur = entity->content_language;
101: HTLanguage pres;
102: while ((pres = (HTLanguage) HTList_nextObject(cur))) {
103: if (first) {
104: PUTS("Content-Language: ");
105: first = NO;
106: } else
107: PUTC(',');
108: PUTS(HTAtom_name(pres));
109: }
110: if (!first) PUTBLOCK(crlf, 2);
2.1 frystyk 111: }
2.34 frystyk 112:
113: /* Only send out Content-Length if we don't have a transfer coding */
114: if (!HTRequest_transfer(request)) {
115: if (EntityMask & HT_E_CONTENT_LENGTH) {
116: if (entity->content_length >= 0) {
117: sprintf(linebuf, "Content-Length: %ld%c%c",
118: entity->content_length, CR, LF);
119: PUTBLOCK(linebuf, (int) strlen(linebuf));
120: } else {
121: transfer_coding = YES;
122: }
2.20 frystyk 123: }
2.1 frystyk 124: }
2.33 frystyk 125: if (EntityMask & HT_E_CONTENT_TYPE && entity->content_type) {
126: HTFormat format = entity->content_type != WWW_UNKNOWN ?
127: entity->content_type : WWW_BINARY;
2.19 frystyk 128: HTAssocList * parameters = HTAnchor_formatParam(entity);
129:
130: /* Output the content type */
131: PUTS("Content-Type: ");
2.33 frystyk 132: PUTS(HTAtom_name(format));
2.19 frystyk 133:
134: /* Add all parameters */
135: if (parameters) {
136: HTAssoc * pres;
137: while ((pres = (HTAssoc *) HTAssocList_nextObject(parameters))) {
138: PUTS(";");
139: PUTS(HTAssoc_name(pres));
140: PUTS("=");
141: PUTS(HTAssoc_value(pres));
142: }
143: }
144: PUTBLOCK(crlf, 2);
2.10 frystyk 145: }
2.21 frystyk 146: if (EntityMask & HT_E_DERIVED_FROM && entity->derived_from) {
2.10 frystyk 147: sprintf(linebuf, "Derived-From: %s%c%c", entity->derived_from,
148: CR, LF);
2.2 frystyk 149: PUTBLOCK(linebuf, (int) strlen(linebuf));
2.1 frystyk 150: }
2.21 frystyk 151: if (EntityMask & HT_E_EXPIRES) {
2.10 frystyk 152: if (entity->expires != -1) {
153: sprintf(linebuf, "Expires: %s%c%c",
154: HTDateTimeStr(&entity->expires, NO), CR,LF);
2.2 frystyk 155: PUTBLOCK(linebuf, (int) strlen(linebuf));
2.1 frystyk 156: }
2.10 frystyk 157: }
2.21 frystyk 158: if (EntityMask & HT_E_LAST_MODIFIED) {
2.10 frystyk 159: if (entity->last_modified != -1) {
160: sprintf(linebuf, "Last-Modified: %s%c%c",
161: HTDateTimeStr(&entity->last_modified, NO), CR,LF);
2.2 frystyk 162: PUTBLOCK(linebuf, (int) strlen(linebuf));
2.1 frystyk 163: }
2.10 frystyk 164: }
2.21 frystyk 165: if (EntityMask & HT_E_LINK) { /* @@@@@@@@@@ */
2.1 frystyk 166:
2.10 frystyk 167: }
2.21 frystyk 168: if (EntityMask & HT_E_TITLE) { /* @@@@@@@@@@ */
2.1 frystyk 169:
2.10 frystyk 170: }
2.21 frystyk 171: if (EntityMask & HT_E_URI) { /* @@@@@@@@@@ */
2.1 frystyk 172:
173: }
2.21 frystyk 174: if (EntityMask & HT_E_VERSION && entity->version) {
2.10 frystyk 175: sprintf(linebuf, "Version: %s%c%c", entity->version, CR, LF);
176: PUTBLOCK(linebuf, (int) strlen(linebuf));
177: }
178: if (me->endHeader) {
179: sprintf(linebuf, "%c%c", CR, LF); /* Blank line means "end" */
180: PUTBLOCK(linebuf, (int) strlen(linebuf));
2.2 frystyk 181: }
2.20 frystyk 182:
183: /*
2.34 frystyk 184: ** Handle any Transfer encoding. This is really a transport issue but
185: ** as it often pops up when we are sending an entity then we put it
186: ** here for now. A better place whould be in the HTTPGen stream.
187: ** the real problem is that the server doesn't have any mechanism of
188: ** telling the client what transports it can handle. The best we can
189: ** hope for is that the server understands "chunked" although we are
190: ** certainly capable of handling nested encodings :(
191: */
192: if (transfer_coding) {
193: HTStream * target = HTTransferCodingStack(WWW_CODING_CHUNKED,
194: me->target, request, NULL, NO);
195: if (STREAM_TRACE) HTTrace("Building.... Transfer-Encoding stack\n");
196: if (target == HTBlackHole()) {
197: if (me->target) (*me->target->isa->abort)(me->target, NULL);
198: me->target = HTErrorStream();
199: } else
200: me->target = target;
201: }
202:
203: #if 0
204: /*
205: ** We expect the anchor object already to have the right encoding and
206: ** we therefore should not set up extra streams for doing this.
2.20 frystyk 207: */
208:
2.34 frystyk 209: /* Handle any Content Transfer encoding */
2.20 frystyk 210: {
2.34 frystyk 211: HTEncoding cte = HTAnchor_contentTransferEncoding(entity);
212: if (!HTFormat_isUnityTransfer(cte)) {
2.20 frystyk 213: if (STREAM_TRACE) HTTrace("Building.... C-T-E stack\n");
2.34 frystyk 214: me->target = HTContentTransferCodingStack(cte, me->target,
215: request, NULL, YES);
2.20 frystyk 216: }
217: }
218:
2.34 frystyk 219: /* Handle any Content Encodings */
2.20 frystyk 220: {
221: HTList * cc = HTAnchor_encoding(entity);
222: if (cc) {
223: if (STREAM_TRACE) HTTrace("Building.... C-E stack\n");
2.33 frystyk 224: me->target = HTContentEncodingStack(cc, me->target, request, NULL);
2.20 frystyk 225: }
226: }
2.34 frystyk 227: #endif
2.20 frystyk 228:
2.13 eric 229: if (PROT_TRACE) HTTrace("MIME........ Generating Entity Headers\n");
2.2 frystyk 230: return HT_OK;
2.1 frystyk 231: }
232:
2.32 frystyk 233: /*
234: ** Flushes header but doesn't free stream object
235: */
236: PRIVATE int MIMERequest_flush (HTStream * me)
237: {
238: int status = MIMERequest_put_block(me, NULL, 0);
239: return status==HT_OK ? (*me->target->isa->flush)(me->target) : status;
240: }
241:
2.14 frystyk 242: PRIVATE int MIMERequest_put_block (HTStream * me, const char * b, int l)
2.1 frystyk 243: {
2.23 frystyk 244: HTNet * net = HTRequest_net(me->request);
2.32 frystyk 245: if (!me->transparent) {
2.1 frystyk 246: MIMEMakeRequest(me, me->request);
2.9 frystyk 247: me->transparent = YES;
2.22 frystyk 248:
249: /*
2.32 frystyk 250: ** First we only send the header
2.22 frystyk 251: */
252: if (HTMethod_hasEntity(HTRequest_method(me->request))) {
253: HTHost * host = HTNet_host(net);
254: char * class = HTHost_class(host);
2.23 frystyk 255: if (class && !strcmp(class, "http")) {
2.32 frystyk 256: MIMERequest_flush(me);
257: return HT_PAUSE;
2.22 frystyk 258: }
259: }
2.1 frystyk 260: }
2.32 frystyk 261:
2.23 frystyk 262: /* Check if we have written it all */
263: if (b) {
264: HTParentAnchor * entity = HTRequest_entityAnchor(me->request);
265: long cl = HTAnchor_length(entity);
266: return (cl>=0 && HTNet_bytesWritten(net) >= cl) ?
267: HT_LOADED : PUTBLOCK(b, l);
268: }
269: return HT_OK;
2.1 frystyk 270: }
271:
272: PRIVATE int MIMERequest_put_character (HTStream * me, char c)
273: {
274: return MIMERequest_put_block(me, &c, 1);
275: }
276:
2.14 frystyk 277: PRIVATE int MIMERequest_put_string (HTStream * me, const char * s)
2.1 frystyk 278: {
279: return MIMERequest_put_block(me, s, strlen(s));
280: }
281:
282: /*
283: ** Flushes data and frees stream object
284: */
285: PRIVATE int MIMERequest_free (HTStream * me)
286: {
287: int status = MIMERequest_flush(me);
288: if (status != HT_WOULD_BLOCK) {
289: if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
290: return HT_WOULD_BLOCK;
2.12 frystyk 291: HT_FREE(me);
2.1 frystyk 292: }
293: return status;
294: }
295:
2.4 frystyk 296: PRIVATE int MIMERequest_abort (HTStream * me, HTList * e)
2.1 frystyk 297: {
298: if (me->target) (*me->target->isa->abort)(me->target, e);
2.12 frystyk 299: HT_FREE(me);
2.13 eric 300: if (PROT_TRACE) HTTrace("MIMERequest. ABORTING...\n");
2.1 frystyk 301: return HT_ERROR;
302: }
303:
304: /* MIMERequest Stream
305: ** -----------------
306: */
2.14 frystyk 307: PRIVATE const HTStreamClass MIMERequestClass =
2.1 frystyk 308: {
309: "MIMERequest",
310: MIMERequest_flush,
311: MIMERequest_free,
312: MIMERequest_abort,
313: MIMERequest_put_character,
314: MIMERequest_put_string,
315: MIMERequest_put_block
316: };
317:
2.10 frystyk 318: PUBLIC HTStream * HTMIMERequest_new (HTRequest * request, HTStream * target,
319: BOOL endHeader)
2.1 frystyk 320: {
2.12 frystyk 321: HTStream * me;
322: if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
323: HT_OUTOFMEM("HTMIMERequest_new");
2.1 frystyk 324: me->isa = &MIMERequestClass;
325: me->target = target;
326: me->request = request;
2.10 frystyk 327: me->endHeader = endHeader;
2.1 frystyk 328: me->transparent = NO;
329: return me;
330: }
Webmaster