You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
7.3 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-stream.h,v 1.10 2006/03/06 13:37:38 m0gg Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003-2004 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ferhatayaz@yahoo.com
  23. ******************************************************************/
  24. #ifndef NANO_HTTP_STREAM_H
  25. #define NANO_HTTP_STREAM_H
  26. #include <stdio.h>
  27. #include <nanohttp/nanohttp-socket.h>
  28. #include <nanohttp/nanohttp-common.h>
  29. /*
  30. HTTP Stream modul:
  31. nanohttp supports 2 different streams:
  32. 1. http_input_stream_t
  33. 2. http_output_stream_t
  34. These are not regular streams. They will care about
  35. transfer styles while sending/receiving data.
  36. Supported transfer styles are
  37. o Content-length
  38. o Chunked encoding
  39. o Connection: "close"
  40. A stream will set its transfer style from the header
  41. information, which must be given while creating a stream.
  42. A stream will start sending/receiving data "after"
  43. sending/receiving header information. (After <CF><CF>)"
  44. */
  45. /**
  46. Transfer types supported while
  47. sending/receiving data.
  48. */
  49. typedef enum http_transfer_type
  50. {
  51. /** The stream cares about Content-length */
  52. HTTP_TRANSFER_CONTENT_LENGTH,
  53. /** The stream sends/receives chunked data */
  54. HTTP_TRANSFER_CHUNKED,
  55. /** The stream sends/receives data until
  56. connection is closed */
  57. HTTP_TRANSFER_CONNECTION_CLOSE,
  58. /** This transfer style will be used by MIME support
  59. and for debug purposes.*/
  60. HTTP_TRANSFER_FILE
  61. } http_transfer_type_t;
  62. /**
  63. HTTP INPUT STREAM. Receives data from a socket/file
  64. and cares about the transfer style.
  65. */
  66. typedef struct http_input_stream
  67. {
  68. hsocket_t *sock;
  69. herror_t err;
  70. http_transfer_type_t type;
  71. int received;
  72. int content_length;
  73. int chunk_size;
  74. byte_t connection_closed;
  75. /* file handling */
  76. FILE *fd;
  77. char filename[255];
  78. int deleteOnExit; /* default is 0 */
  79. } http_input_stream_t;
  80. /**
  81. HTTP OUTPUT STREAM. Sends data to a socket
  82. and cares about the transfer style.
  83. */
  84. typedef struct http_output_stream
  85. {
  86. hsocket_t *sock;
  87. http_transfer_type_t type;
  88. int content_length;
  89. int sent;
  90. } http_output_stream_t;
  91. #ifdef __cplusplus
  92. extern "C" {
  93. #endif
  94. /*
  95. --------------------------------------------------------------
  96. HTTP INPUT STREAM
  97. --------------------------------------------------------------
  98. */
  99. /**
  100. Creates a new input stream. The transfer style will be
  101. choosen from the given header.
  102. @param sock the socket to receive data from
  103. @param header the http header. This must be received before
  104. creating a http_input_stream_t.
  105. @returns a newly created http_input_stream_t object. If no
  106. transfer style was found in the header,
  107. HTTP_TRANSFER_CONNECTION_CLOSE will be used as default.
  108. @see http_input_stream_free
  109. */
  110. http_input_stream_t *http_input_stream_new(hsocket_t *sock, hpair_t *header);
  111. /**
  112. Creates a new input stream from file.
  113. This function was added for MIME messages
  114. and for debugging. The transfer style is always
  115. HTTP_TRANSFER_FILE.
  116. @param filename the name of the file to open and read.
  117. @returns The return value is a http_input_stream_t object
  118. if the file exists and could be opened. NULL otherwise.
  119. @see http_input_stream_free
  120. */
  121. http_input_stream_t *http_input_stream_new_from_file(const char *filename);
  122. /**
  123. Free input stream. Note that the socket will not be closed
  124. by this functions.
  125. @param stream the input stream to free.
  126. */
  127. void http_input_stream_free(http_input_stream_t * stream);
  128. /**
  129. Returns the actual status of the stream.
  130. @param stream the stream to check its status
  131. @returns <br>1 if there are still data to read.
  132. <br>0 if no more data exists.
  133. */
  134. int http_input_stream_is_ready(http_input_stream_t * stream);
  135. /**
  136. Tries to read 'size' bytes from the stream. Check always
  137. with http_input_stream_is_ready() if there are some data
  138. to read. If it returns 0, no more data is available on
  139. stream.
  140. <P>
  141. On error this function will return -1. In this case the
  142. "err" field of stream will be set to the actual error.
  143. This can be one of the followings: <P>
  144. <BR>STREAM_ERROR_NO_CHUNK_SIZE
  145. <BR>STREAM_ERROR_WRONG_CHUNK_SIZE
  146. <BR>STREAM_ERROR_INVALID_TYPE
  147. <BR>HSOCKET_ERROR_RECEIVE
  148. @param stream the stream to read data from
  149. @param dest destination memory to store readed bytes
  150. @param size maximum size of 'dest' (size to read)
  151. @returns the actual readed bytes or -1 on error.
  152. */
  153. int http_input_stream_read(http_input_stream_t * stream,
  154. byte_t * dest, int size);
  155. /*
  156. --------------------------------------------------------------
  157. HTTP OUTPUT STREAM
  158. --------------------------------------------------------------
  159. */
  160. /**
  161. Creates a new output stream. Transfer style will be found
  162. from the given header.
  163. @param sock the socket to to send data to
  164. @param header the header which must be sent before
  165. @returns a http_output_stream_t object. If no proper transfer
  166. style was found in the header, HTTP_TRANSFER_CONNECTION_CLOSE
  167. will be used as default.
  168. @see http_output_stream_free
  169. */
  170. http_output_stream_t *http_output_stream_new(hsocket_t *sock,
  171. hpair_t * header);
  172. /**
  173. Free output stream. Note that this functions will not
  174. close any socket connections.
  175. @param stream the stream to free.
  176. */
  177. void http_output_stream_free(http_output_stream_t * stream);
  178. /**
  179. Writes 'size' bytes of 'bytes' into stream.
  180. @param stream the stream to use to send data
  181. @param bytes bytes to send
  182. @param size size of bytes to send
  183. @returns H_OK if success. One of the followings otherwise
  184. <BR>HSOCKET_ERROR_NOT_INITIALIZED
  185. <BR>HSOCKET_ERROR_SEND
  186. */
  187. herror_t http_output_stream_write(http_output_stream_t * stream,
  188. const byte_t * bytes, int size);
  189. /**
  190. Writes a null terminated string to the stream.
  191. @param stream the stream to use to send data
  192. @param str a null terminated string to send
  193. @returns H_OK if success. One of the followings otherwise
  194. <BR>HSOCKET_ERROR_NOT_INITIALIZED
  195. <BR>HSOCKET_ERROR_SEND
  196. */
  197. herror_t http_output_stream_write_string(http_output_stream_t * stream,
  198. const char *str);
  199. /**
  200. Sends finish flags if nesseccary (like in chunked transport).
  201. Call always this function before closing the connections.
  202. @param stream the stream to send post data.
  203. @returns H_OK if success. One of the followings otherwise
  204. <BR>HSOCKET_ERROR_NOT_INITIALIZED
  205. <BR>HSOCKET_ERROR_SEND
  206. */
  207. herror_t http_output_stream_flush(http_output_stream_t * stream);
  208. #ifdef __cplusplus
  209. }
  210. #endif
  211. #endif