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.

322 lines
7.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-request.c,v 1.14 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C
  5. * Copyright (C) 2003 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: ayaz@jprogrammer.net
  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-request.h"
  42. static hrequest_t *
  43. hrequest_new(void)
  44. {
  45. hrequest_t *req;
  46. if (!(req = (hrequest_t *) malloc(sizeof(hrequest_t)))) {
  47. log_error2("malloc failed (%s)", strerror(errno));
  48. return NULL;
  49. }
  50. req->method = HTTP_REQUEST_GET;
  51. req->version = HTTP_1_1;
  52. req->query = NULL;
  53. req->header = NULL;
  54. req->in = NULL;
  55. req->attachments = NULL;
  56. req->content_type = NULL;
  57. return req;
  58. }
  59. static hrequest_t *
  60. _hrequest_parse_header(char *data)
  61. {
  62. hrequest_t *req;
  63. hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL;
  64. char *tmp;
  65. char *tmp2;
  66. char *saveptr;
  67. char *saveptr2;
  68. char *saveptr3;
  69. char *result;
  70. char *key;
  71. char *opt_key;
  72. char *opt_value;
  73. int firstline = 1;
  74. req = hrequest_new();
  75. tmp = data;
  76. for (;;)
  77. {
  78. result = (char *) strtok_r(tmp, "\r\n", &saveptr);
  79. tmp = saveptr;
  80. if (result == NULL)
  81. break;
  82. if (firstline)
  83. {
  84. firstline = 0;
  85. tmp2 = result;
  86. /* parse [GET|POST] [PATH] [SPEC] */
  87. key = (char *) strtok_r(tmp2, " ", &saveptr2);
  88. /* save method (get or post) */
  89. tmp2 = saveptr2;
  90. if (key != NULL)
  91. {
  92. if (!strcmp(key, "POST"))
  93. req->method = HTTP_REQUEST_POST;
  94. else if (!strcmp(key, "GET"))
  95. req->method = HTTP_REQUEST_GET;
  96. else if (!strcmp(key, "OPTIONS"))
  97. req->method = HTTP_REQUEST_OPTIONS;
  98. else if (!strcmp(key, "HEAD"))
  99. req->method = HTTP_REQUEST_HEAD;
  100. else if (!strcmp(key, "PUT"))
  101. req->method = HTTP_REQUEST_PUT;
  102. else if (!strcmp(key, "DELETE"))
  103. req->method = HTTP_REQUEST_DELETE;
  104. else if (!strcmp(key, "TRACE"))
  105. req->method = HTTP_REQUEST_TRACE;
  106. else if (!strcmp(key, "CONNECT"))
  107. req->method = HTTP_REQUEST_CONNECT;
  108. else
  109. req->method = HTTP_REQUEST_UNKOWN;
  110. }
  111. /* below is key the path and tmp2 the spec */
  112. key = (char *) strtok_r(tmp2, " ", &saveptr2);
  113. /* save version */
  114. tmp2 = saveptr2;
  115. if (tmp2 != NULL)
  116. {
  117. /* req->spec = (char *) malloc(strlen(tmp2) + 1); strcpy(req->spec,
  118. tmp2); */
  119. if (!strcmp(tmp2, "HTTP/1.0"))
  120. req->version = HTTP_1_0;
  121. else
  122. req->version = HTTP_1_1;
  123. }
  124. /*
  125. * parse and save path+query parse:
  126. * /path/of/target?key1=value1&key2=value2...
  127. */
  128. if (key != NULL)
  129. {
  130. tmp2 = key;
  131. key = (char *) strtok_r(tmp2, "?", &saveptr2);
  132. tmp2 = saveptr2;
  133. /* save path */
  134. /* req->path = (char *) malloc(strlen(key) + 1); */
  135. strncpy(req->path, key, REQUEST_MAX_PATH_SIZE);
  136. /* parse options */
  137. for (;;)
  138. {
  139. key = (char *) strtok_r(tmp2, "&", &saveptr2);
  140. tmp2 = saveptr2;
  141. if (key == NULL)
  142. break;
  143. opt_key = (char *) strtok_r(key, "=", &saveptr3);
  144. opt_value = saveptr3;
  145. if (opt_value == NULL)
  146. opt_value = "";
  147. /* create option pair */
  148. if (opt_key != NULL)
  149. {
  150. if (!(tmppair = (hpair_t *) malloc(sizeof(hpair_t))))
  151. {
  152. log_error2("malloc failed (%s)", strerror(errno));
  153. return NULL;
  154. }
  155. if (req->query == NULL)
  156. {
  157. req->query = qpair = tmppair;
  158. }
  159. else
  160. {
  161. qpair->next = tmppair;
  162. qpair = tmppair;
  163. }
  164. /* fill hpairnode_t struct */
  165. qpair->next = NULL;
  166. qpair->key = strdup(opt_key);
  167. qpair->value = strdup(opt_value);
  168. }
  169. }
  170. }
  171. }
  172. else
  173. {
  174. /* parse "key: value" */
  175. /* tmp2 = result; key = (char *) strtok_r(tmp2, ": ", &saveptr2); value
  176. = saveptr2; */
  177. /* create pair */
  178. /* tmppair = (hpair_t *) malloc(sizeof(hpair_t));*/
  179. tmppair = hpairnode_parse(result, ":", NULL);
  180. if (req->header == NULL)
  181. {
  182. req->header = hpair = tmppair;
  183. }
  184. else
  185. {
  186. hpair->next = tmppair;
  187. hpair = tmppair;
  188. }
  189. /* fill pairnode_t struct */
  190. /*
  191. hpair->next = NULL; hpair->key = (char *) malloc(strlen(key) + 1);
  192. hpair->value = (char *) malloc(strlen(value) + 1);
  193. strcpy(hpair->key, key); strcpy(hpair->value, value); */
  194. }
  195. }
  196. /* Check Content-type */
  197. tmp = hpairnode_get_ignore_case(req->header, HEADER_CONTENT_TYPE);
  198. if (tmp != NULL)
  199. req->content_type = content_type_new(tmp);
  200. return req;
  201. }
  202. void
  203. hrequest_free(hrequest_t * req)
  204. {
  205. if (req == NULL)
  206. return;
  207. hpairnode_free_deep(req->header);
  208. hpairnode_free_deep(req->query);
  209. if (req->in)
  210. http_input_stream_free(req->in);
  211. if (req->content_type)
  212. content_type_free(req->content_type);
  213. if (req->attachments)
  214. attachments_free(req->attachments);
  215. free(req);
  216. return;
  217. }
  218. herror_t
  219. hrequest_new_from_socket(hsocket_t *sock, hrequest_t ** out)
  220. {
  221. int i, readed;
  222. herror_t status;
  223. hrequest_t *req;
  224. char buffer[MAX_HEADER_SIZE + 1];
  225. attachments_t *mimeMessage;
  226. memset(buffer, 0, MAX_HEADER_SIZE);
  227. /* Read header */
  228. for(i=0; i < MAX_HEADER_SIZE; i++)
  229. {
  230. if ((status = hsocket_read(sock, &(buffer[i]), 1, 1, &readed)) != H_OK)
  231. {
  232. log_error2("hsocket_read failed (%s)", herror_message(status));
  233. return status;
  234. }
  235. buffer[i + 1] = '\0'; /* for strmp */
  236. /* log_error2("buffer=\"%s\"", buffer); */
  237. if (i > 3)
  238. {
  239. if (!strcmp(&(buffer[i - 1]), "\n\n") ||
  240. !strcmp(&(buffer[i - 2]), "\n\r\n"))
  241. break;
  242. }
  243. }
  244. /* Create response */
  245. req = _hrequest_parse_header(buffer);
  246. /* Create input stream */
  247. req->in = http_input_stream_new(sock, req->header);
  248. /* Check for MIME message */
  249. if ((req->content_type &&
  250. !strcmp(req->content_type->type, "multipart/related")))
  251. {
  252. status = mime_get_attachments(req->content_type, req->in, &mimeMessage);
  253. if (status != H_OK)
  254. {
  255. /* TODO (#1#): Handle error */
  256. hrequest_free(req);
  257. return status;
  258. }
  259. else
  260. {
  261. req->attachments = mimeMessage;
  262. req->in =
  263. http_input_stream_new_from_file(mimeMessage->root_part->filename);
  264. }
  265. }
  266. *out = req;
  267. return H_OK;
  268. }