HTMIMERq.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:6k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /*      HTMIMERq.c
  2. ** MIME ENTITY HEADERS GENERATION
  3. **
  4. ** This module implements the output stream for MIME used for sending
  5. ** requests with a entity body to HTTP, NEWS, etc. or for generating
  6. ** responses
  7. **
  8. ** History:
  9. ** Jan 95 HFN Written
  10. */
  11. /* Library Includes */
  12. #include "tcp.h"
  13. #include "HTUtils.h"
  14. #include "HTString.h"
  15. #include "HTWWWStr.h"
  16. #include "HTParse.h"
  17. #include "HTFormat.h"
  18. #include "HTAncMan.h"
  19. #include "HTNetMan.h"
  20. #include "HTDNS.h"
  21. #include "HTTCP.h"
  22. #include "HTWriter.h"
  23. #include "HTHeader.h"
  24. #include "HTReqMan.h"
  25. #include "HTTPReq.h"        /* Implements */
  26. #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
  27. struct _HTStream {
  28.     CONST HTStreamClass * isa;
  29.     HTStream *    target;
  30.     HTRequest * request;
  31.     BOOL endHeader;
  32.     BOOL transparent;
  33. };
  34. /* ------------------------------------------------------------------------- */
  35. /*      MIME Output Request Stream      */
  36. /* ------------------------------------------------------------------------- */
  37. /* MIMEMakeRequest
  38. ** ---------------
  39. ** Generates the BODY parts of a MIME message.
  40. */
  41. PRIVATE int MIMEMakeRequest (HTStream * me, HTRequest * request)
  42. {
  43.     char linebuf[256]; /* @@@ */
  44.     HTParentAnchor *entity = request->source_anchor ?
  45. request->source_anchor : request->anchor;
  46.     if (request->EntityMask & HT_E_ALLOW) {
  47. /* @@@@@@@@@@ */
  48.     }
  49.     if (request->EntityMask & HT_E_CONTENT_ENCODING &&
  50. entity->content_encoding) {
  51. sprintf(linebuf, "Content-Encoding: %s%c%c",
  52. HTAtom_name(entity->content_encoding), CR, LF);
  53. PUTBLOCK(linebuf, (int) strlen(linebuf));
  54.     }
  55.     
  56.     /* @@@ SHOULD BE A LIST @@@ */
  57.     if (request->EntityMask & HT_E_CONTENT_LANGUAGE &&
  58. entity->content_language) {
  59. sprintf(linebuf, "Content-Language: %s%c%c",
  60. HTAtom_name(entity->content_language), CR, LF);
  61. PUTBLOCK(linebuf, (int) strlen(linebuf));
  62.     }
  63.     if (request->EntityMask & HT_E_CONTENT_LENGTH) { /* Must be there!!! */
  64. sprintf(linebuf, "Content-Length: %ld%c%c",
  65. entity->content_length, CR, LF);
  66. PUTBLOCK(linebuf, (int) strlen(linebuf));
  67.     }
  68.     if (request->EntityMask & HT_E_CTE && entity->cte) {
  69. sprintf(linebuf, "Content-Transfer-Encoding: %s%c%c",
  70. HTAtom_name(entity->cte), CR, LF);
  71. PUTBLOCK(linebuf, (int) strlen(linebuf));
  72.     }
  73.     if (request->EntityMask & HT_E_CONTENT_TYPE && entity->content_type) {
  74. int len;
  75. sprintf(linebuf, "Content-Type: %s",
  76. HTAtom_name(entity->content_type));
  77. if (entity->charset) {
  78.     strcat(linebuf, "; charset=");
  79.     strcat(linebuf, HTAtom_name(entity->charset));
  80. }
  81. if (entity->level) {
  82.     strcat(linebuf, "; level=");
  83.     strcat(linebuf, HTAtom_name(entity->level));
  84. }
  85. len = strlen(linebuf);
  86. *(linebuf+len) = CR;
  87. *(linebuf+len+1) = LF;
  88. *(linebuf+len+2) = '';
  89. PUTBLOCK(linebuf, (int) len+2);
  90.     }
  91.     if (request->EntityMask & HT_E_DERIVED_FROM && entity->derived_from) {
  92. sprintf(linebuf, "Derived-From: %s%c%c", entity->derived_from,
  93. CR, LF);
  94. PUTBLOCK(linebuf, (int) strlen(linebuf));
  95.     }
  96.     if (request->EntityMask & HT_E_EXPIRES) {
  97. if (entity->expires != -1) {
  98.     sprintf(linebuf, "Expires: %s%c%c",
  99.     HTDateTimeStr(&entity->expires, NO), CR,LF);
  100.     PUTBLOCK(linebuf, (int) strlen(linebuf));
  101. }
  102.     }
  103.     if (request->EntityMask & HT_E_LAST_MODIFIED) {
  104. if (entity->last_modified != -1) {
  105.     sprintf(linebuf, "Last-Modified: %s%c%c",
  106.     HTDateTimeStr(&entity->last_modified, NO), CR,LF);
  107.     PUTBLOCK(linebuf, (int) strlen(linebuf));
  108. }
  109.     }
  110.     if (request->EntityMask & HT_E_LINK) { /* @@@@@@@@@@ */
  111.     }
  112.     if (request->EntityMask & HT_E_TITLE) { /* @@@@@@@@@@ */
  113.     }
  114.     if (request->EntityMask & HT_E_URI) { /* @@@@@@@@@@ */
  115.     }
  116.     if (request->EntityMask & HT_E_VERSION && entity->version) {
  117. sprintf(linebuf, "Version: %s%c%c", entity->version, CR, LF);
  118. PUTBLOCK(linebuf, (int) strlen(linebuf));
  119.     }
  120.     if (me->endHeader) {
  121. sprintf(linebuf, "%c%c", CR, LF);    /* Blank line means "end" */
  122. PUTBLOCK(linebuf, (int) strlen(linebuf));
  123.     }
  124.     if (PROT_TRACE) TTYPrint(TDEST,"MIME........ Generating Entity Headersn");
  125.     return HT_OK;
  126. }
  127. PRIVATE int MIMERequest_put_block (HTStream * me, CONST char * b, int l)
  128. {
  129.     if (me->transparent)
  130. return b ? PUTBLOCK(b, l) : HT_OK;
  131.     else {
  132. MIMEMakeRequest(me, me->request);
  133. if (HTRequest_isDestination(me->request)) {
  134.     (*me->target->isa->flush)(me->target);
  135.     HTNet_setBytesWritten(me->request->net, 0);
  136. }
  137. me->transparent = YES;
  138. return b ? PUTBLOCK(b, l) : HT_OK;
  139.     }
  140. }
  141. PRIVATE int MIMERequest_put_character (HTStream * me, char c)
  142. {
  143.     return MIMERequest_put_block(me, &c, 1);
  144. }
  145. PRIVATE int MIMERequest_put_string (HTStream * me, CONST char * s)
  146. {
  147.     return MIMERequest_put_block(me, s, strlen(s));
  148. }
  149. /*
  150. ** Flushes header but doesn't free stream object
  151. */
  152. PRIVATE int MIMERequest_flush (HTStream * me)
  153. {
  154.     int status = MIMERequest_put_block(me, NULL, 0);
  155.     return status==HT_OK ? (*me->target->isa->flush)(me->target) : status;
  156. }
  157. /*
  158. ** Flushes data and frees stream object
  159. */
  160. PRIVATE int MIMERequest_free (HTStream * me)
  161. {
  162.     int status = MIMERequest_flush(me);
  163.     if (status != HT_WOULD_BLOCK) {
  164. if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
  165.     return HT_WOULD_BLOCK;
  166. HT_FREE(me);
  167.     }
  168.     return status;
  169. }
  170. PRIVATE int MIMERequest_abort (HTStream * me, HTList * e)
  171. {
  172.     if (me->target) (*me->target->isa->abort)(me->target, e);
  173.     HT_FREE(me);
  174.     if (PROT_TRACE) TTYPrint(TDEST, "MIMERequest. ABORTING...n");
  175.     return HT_ERROR;
  176. }
  177. /* MIMERequest Stream
  178. ** -----------------
  179. */
  180. PRIVATE CONST HTStreamClass MIMERequestClass =
  181. {
  182.     "MIMERequest",
  183.     MIMERequest_flush,
  184.     MIMERequest_free,
  185.     MIMERequest_abort,
  186.     MIMERequest_put_character,
  187.     MIMERequest_put_string,
  188.     MIMERequest_put_block
  189. };
  190. PUBLIC HTStream * HTMIMERequest_new (HTRequest * request, HTStream * target,
  191.      BOOL endHeader)
  192. {
  193.     HTStream * me;
  194.     if ((me = (HTStream  *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
  195.         HT_OUTOFMEM("HTMIMERequest_new");
  196.     me->isa = &MIMERequestClass;
  197.     me->target = target;
  198.     me->request = request;
  199.     me->endHeader = endHeader;
  200.     me->transparent = NO;
  201.     return me;
  202. }