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.

253 lines
5.5 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-response.c,v 1.11 2006/07/09 16:24:19 snowdrop 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. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifdef HAVE_STDIO_H
  28. #include <stdio.h>
  29. #endif
  30. #ifdef HAVE_STRING_H
  31. #include <string.h>
  32. #endif
  33. #ifdef HAVE_ERRNO_H
  34. #include <errno.h>
  35. #endif
  36. #ifdef MEM_DEBUG
  37. #include <utils/alloc.h>
  38. #endif
  39. #include "nanohttp-logging.h"
  40. #include "nanohttp-common.h"
  41. #include "nanohttp-response.h"
  42. static hresponse_t *
  43. hresponse_new()
  44. {
  45. hresponse_t *res;
  46. /* create response object */
  47. if (!(res = (hresponse_t *) malloc(sizeof(hresponse_t)))) {
  48. log_error2("malloc failed (%s)", strerror(errno));
  49. return NULL;
  50. }
  51. res->version = HTTP_1_1;
  52. res->errcode = -1;
  53. res->desc[0] = '\0';
  54. res->header = NULL;
  55. res->in = NULL;
  56. res->content_type = NULL;
  57. res->attachments = NULL;
  58. return res;
  59. }
  60. static hresponse_t *
  61. _hresponse_parse_header(const char *buffer)
  62. {
  63. hresponse_t *res;
  64. char *s1, *s2, *str;
  65. /* create response object */
  66. res = hresponse_new();
  67. /* *** parse spec *** */
  68. /* [HTTP/1.1 | 1.2] [CODE] [DESC] */
  69. /* stage 1: HTTP spec */
  70. str = (char *) strtok_r((char *) buffer, " ", &s2);
  71. s1 = s2;
  72. if (str == NULL)
  73. {
  74. log_error1("Parse error reading HTTP spec");
  75. return NULL;
  76. }
  77. if (!strcmp(str, "HTTP/1.0"))
  78. res->version = HTTP_1_0;
  79. else
  80. res->version = HTTP_1_1;
  81. /* stage 2: http code */
  82. str = (char *) strtok_r(s1, " ", &s2);
  83. s1 = s2;
  84. if (str == NULL)
  85. {
  86. log_error1("Parse error reading HTTP code");
  87. return NULL;
  88. }
  89. res->errcode = atoi(str);
  90. /* stage 3: description text */
  91. str = (char *) strtok_r(s1, "\r\n", &s2);
  92. s1 = s2;
  93. if (str == NULL)
  94. {
  95. log_error1("Parse error reading HTTP description");
  96. return NULL;
  97. }
  98. /* res->desc = (char *) malloc(strlen(str) + 1);*/
  99. strncpy(res->desc, str, RESPONSE_MAX_DESC_SIZE);
  100. res->desc[strlen(str)] = '\0';
  101. /* *** parse header *** */
  102. /* [key]: [value] */
  103. for (;;)
  104. {
  105. str = strtok_r(s1, "\n", &s2);
  106. s1 = s2;
  107. /* check if header ends without body */
  108. if (str == NULL)
  109. {
  110. return res;
  111. }
  112. /* check also for end of header */
  113. if (!strcmp(str, "\r"))
  114. {
  115. break;
  116. }
  117. str[strlen(str) - 1] = '\0';
  118. res->header = hpairnode_parse(str, ":", res->header);
  119. }
  120. /* Check Content-type */
  121. str = hpairnode_get(res->header, HEADER_CONTENT_TYPE);
  122. if (str != NULL)
  123. res->content_type = content_type_new(str);
  124. /* return response object */
  125. return res;
  126. }
  127. herror_t
  128. hresponse_new_from_socket(hsocket_t *sock, hresponse_t ** out)
  129. {
  130. int i = 0, count;
  131. herror_t status;
  132. hresponse_t *res;
  133. attachments_t *mimeMessage;
  134. char buffer[MAX_HEADER_SIZE + 1];
  135. read_header: /* for errorcode: 100 (continue) */
  136. /* Read header */
  137. while (i < MAX_HEADER_SIZE)
  138. {
  139. if ((status = hsocket_read(sock, &(buffer[i]), 1, 1, &count)) != H_OK)
  140. {
  141. log_error1("Socket read error");
  142. return status;
  143. }
  144. buffer[i + 1] = '\0'; /* for strmp */
  145. if (i > 3)
  146. {
  147. if (!strcmp(&(buffer[i - 1]), "\n\n") ||
  148. !strcmp(&(buffer[i - 2]), "\n\r\n"))
  149. break;
  150. }
  151. i++;
  152. }
  153. /* Create response */
  154. res = _hresponse_parse_header(buffer);
  155. if (res == NULL)
  156. {
  157. log_error1("Header parse error");
  158. return herror_new("hresponse_new_from_socket",
  159. GENERAL_HEADER_PARSE_ERROR,
  160. "Can not parse response header");
  161. }
  162. /* Chec for Errorcode: 100 (continue) */
  163. if (res->errcode == 100)
  164. {
  165. hresponse_free(res);
  166. i = 0;
  167. goto read_header;
  168. }
  169. /* Create input stream */
  170. res->in = http_input_stream_new(sock, res->header);
  171. /* Check for MIME message */
  172. if ((res->content_type &&
  173. !strcmp(res->content_type->type, "multipart/related")))
  174. {
  175. status = mime_get_attachments(res->content_type, res->in, &mimeMessage);
  176. if (status != H_OK)
  177. {
  178. /* TODO (#1#): Handle error */
  179. hresponse_free(res);
  180. return status;
  181. }
  182. else
  183. {
  184. res->attachments = mimeMessage;
  185. http_input_stream_free(res->in);
  186. res->in =
  187. http_input_stream_new_from_file(mimeMessage->root_part->filename);
  188. if (!res->in)
  189. {
  190. /* TODO (#1#): Handle error */
  191. }
  192. else
  193. {
  194. /* res->in->deleteOnExit = 1; */
  195. }
  196. }
  197. }
  198. *out = res;
  199. return H_OK;
  200. }
  201. void
  202. hresponse_free(hresponse_t * res)
  203. {
  204. if (res == NULL)
  205. return;
  206. if (res->header)
  207. hpairnode_free_deep(res->header);
  208. if (res->in)
  209. http_input_stream_free(res->in);
  210. if (res->content_type)
  211. content_type_free(res->content_type);
  212. if (res->attachments)
  213. attachments_free(res->attachments);
  214. free(res);
  215. }