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.

652 lines
15 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: nanohttp-client.c,v 1.41 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: ferhatayaz@yahoo.com
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifdef HAVE_TIME_H
  28. #include <time.h>
  29. #endif
  30. #ifdef HAVE_STDIO_H
  31. #include <stdio.h>
  32. #endif
  33. #ifdef HAVE_STDLIB_H
  34. #include <stdlib.h>
  35. #endif
  36. #ifdef HAVE_STDARG_H
  37. #include <stdarg.h>
  38. #endif
  39. #ifdef HAVE_STRING_H
  40. #include <string.h>
  41. #endif
  42. #ifdef MEM_DEBUG
  43. #include <utils/alloc.h>
  44. #endif
  45. #include "nanohttp-client.h"
  46. #include "nanohttp-socket.h"
  47. #include "nanohttp-base64.h"
  48. #include "nanohttp-logging.h"
  49. /*--------------------------------------------------
  50. FUNCTION: httpc_init
  51. DESC: Initialize http client connection
  52. NOTE: This will be called from soap_client_init_args()
  53. ----------------------------------------------------*/
  54. herror_t
  55. httpc_init(int argc, char **argv)
  56. {
  57. return hsocket_module_init(argc, argv);
  58. }
  59. /*--------------------------------------------------
  60. FUNCTION: httpc_destroy
  61. DESC: Destroy the http client module
  62. ----------------------------------------------------*/
  63. void
  64. httpc_destroy(void)
  65. {
  66. hsocket_module_destroy();
  67. return;
  68. }
  69. /*--------------------------------------------------
  70. FUNCTION: httpc_new
  71. DESC: Creates a new http client connection object
  72. You need to create at least 1 http client connection
  73. to communicate via http.
  74. ----------------------------------------------------*/
  75. httpc_conn_t *
  76. httpc_new(void)
  77. {
  78. static int counter = 10000;
  79. herror_t status;
  80. httpc_conn_t *res;
  81. if (!(res = (httpc_conn_t *) malloc(sizeof(httpc_conn_t))))
  82. return NULL;
  83. if ((status = hsocket_init(&res->sock)) != H_OK)
  84. {
  85. log_warn2("hsocket_init failed (%s)", herror_message(status));
  86. return NULL;
  87. }
  88. res->header = NULL;
  89. res->version = HTTP_1_1;
  90. res->out = NULL;
  91. res->_dime_package_nr = 0;
  92. res->_dime_sent_bytes = 0;
  93. res->id = counter++;
  94. return res;
  95. }
  96. /*--------------------------------------------------
  97. FUNCTION: httpc_free
  98. DESC: Free the given http client object.
  99. ----------------------------------------------------*/
  100. void
  101. httpc_free(httpc_conn_t * conn)
  102. {
  103. hpair_t *tmp;
  104. if (conn == NULL)
  105. return;
  106. while (conn->header != NULL)
  107. {
  108. tmp = conn->header;
  109. conn->header = conn->header->next;
  110. hpairnode_free(tmp);
  111. }
  112. if (conn->out != NULL)
  113. {
  114. http_output_stream_free(conn->out);
  115. conn->out = NULL;
  116. }
  117. hsocket_free(&(conn->sock));
  118. free(conn);
  119. return;
  120. }
  121. /*--------------------------------------------------
  122. FUNCTION: httpc_close_free
  123. DESC: Close and free the given http client object.
  124. ----------------------------------------------------*/
  125. void
  126. httpc_close_free(httpc_conn_t * conn)
  127. {
  128. if (conn == NULL)
  129. return;
  130. hsocket_close(&(conn->sock));
  131. httpc_free(conn);
  132. return;
  133. }
  134. int
  135. httpc_add_header(httpc_conn_t *conn, const char *key, const char *value)
  136. {
  137. if (!conn)
  138. {
  139. log_warn1("Connection object is NULL");
  140. return -1;
  141. }
  142. conn->header = hpairnode_new(key, value, conn->header);
  143. return 0;
  144. }
  145. void
  146. httpc_add_headers(httpc_conn_t *conn, const hpair_t *values)
  147. {
  148. if (conn == NULL)
  149. {
  150. log_warn1("Connection object is NULL");
  151. return;
  152. }
  153. for ( ;values; values=values->next)
  154. httpc_add_header(conn, values->key, values->value);
  155. return;
  156. }
  157. /*--------------------------------------------------
  158. FUNCTION: httpc_set_header
  159. DESC: Adds a new (key, value) pair to the header
  160. or modifies the old pair if this function will
  161. finds another pair with the same 'key' value.
  162. ----------------------------------------------------*/
  163. int
  164. httpc_set_header(httpc_conn_t *conn, const char *key, const char *value)
  165. {
  166. hpair_t *p;
  167. if (conn == NULL)
  168. {
  169. log_warn1("Connection object is NULL");
  170. return 0;
  171. }
  172. for (p = conn->header; p; p = p->next)
  173. {
  174. if (p->key && !strcmp(p->key, key))
  175. {
  176. free(p->value);
  177. p->value = strdup(value);
  178. return 1;
  179. }
  180. }
  181. conn->header = hpairnode_new(key, value, conn->header);
  182. return 0;
  183. }
  184. static int
  185. _httpc_set_basic_authorization_header(httpc_conn_t *conn, const char *key, const char *user, const char *password)
  186. {
  187. /* XXX: use malloc/free */
  188. char in[64], out[64];
  189. if (!user)
  190. user = "";
  191. if (!password)
  192. password = "";
  193. memset(in, 0, 64);
  194. memset(out, 0, 64);
  195. sprintf(in, "%s:%s", user, password);
  196. base64_encode(in, out);
  197. sprintf(in, "Basic %s", out);
  198. return httpc_set_header(conn, key, in);
  199. }
  200. int
  201. httpc_set_basic_authorization(httpc_conn_t *conn, const char *user, const char *password)
  202. {
  203. return _httpc_set_basic_authorization_header(conn, HEADER_AUTHORIZATION, user, password);
  204. }
  205. int
  206. httpc_set_basic_proxy_authorization(httpc_conn_t *conn, const char *user, const char *password)
  207. {
  208. return _httpc_set_basic_authorization_header(conn, HEADER_PROXY_AUTHORIZATION, user, password);
  209. }
  210. /*--------------------------------------------------
  211. FUNCTION: httpc_header_set_date
  212. DESC: Adds the current date to the header.
  213. ----------------------------------------------------*/
  214. static void
  215. httpc_header_set_date(httpc_conn_t * conn)
  216. {
  217. char buffer[32];
  218. time_t ts;
  219. struct tm stm;
  220. ts = time(NULL);
  221. localtime_r(&ts, &stm);
  222. strftime(buffer, 32, "%a, %d %b %Y %H:%M:%S GMT", &stm);
  223. httpc_set_header(conn, HEADER_DATE, buffer);
  224. return;
  225. }
  226. /*--------------------------------------------------
  227. FUNCTION: httpc_send_header
  228. DESC: Sends the current header information stored
  229. in conn through conn->sock.
  230. ----------------------------------------------------*/
  231. herror_t
  232. httpc_send_header(httpc_conn_t * conn)
  233. {
  234. hpair_t *walker;
  235. herror_t status;
  236. char buffer[1024];
  237. for (walker = conn->header; walker; walker = walker->next)
  238. {
  239. if (walker->key && walker->value)
  240. {
  241. sprintf(buffer, "%s: %s\r\n", walker->key, walker->value);
  242. if ((status = hsocket_send(&(conn->sock), buffer)) != H_OK)
  243. return status;
  244. }
  245. }
  246. return hsocket_send(&(conn->sock), "\r\n");
  247. }
  248. /*--------------------------------------------------
  249. FUNCTION: httpc_talk_to_server
  250. DESC: This function is the heart of the httpc
  251. module. It will send the request and process the
  252. response.
  253. Here the parameters:
  254. method:
  255. the request method. This can be HTTP_REQUEST_POST and
  256. HTTP_REQUEST_GET.
  257. conn:
  258. the connection object (created with httpc_new())
  259. urlstr:
  260. the complete url in string format.
  261. http://<host>:<port>/<context>
  262. where <port> is not mendatory.
  263. start_cb:
  264. a callback function, which will be called when
  265. the response header is completely arrives.
  266. cb:
  267. a callback function, which will be called everytime
  268. when data arrives.
  269. content_size:
  270. size of content to send.
  271. (only if method is HTTP_REQUEST_POST)
  272. content:
  273. the content data to send.
  274. (only if method is HTTP_REQUEST_POST)
  275. userdata:
  276. a user define data, which will be passed to the
  277. start_cb and cb callbacks as a parameter. This
  278. can also be NULL.
  279. If success, this function will return 0.
  280. >0 otherwise.
  281. ----------------------------------------------------*/
  282. static herror_t
  283. httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn,
  284. const char *urlstr)
  285. {
  286. hurl_t url;
  287. char buffer[4096];
  288. herror_t status;
  289. int ssl;
  290. if (conn == NULL)
  291. {
  292. return herror_new("httpc_talk_to_server",
  293. GENERAL_INVALID_PARAM, "httpc_conn_t param is NULL");
  294. }
  295. /* Build request header */
  296. httpc_header_set_date(conn);
  297. if ((status = hurl_parse(&url, urlstr)) != H_OK)
  298. {
  299. log_error2("Can not parse URL '%s'", SAVE_STR(urlstr));
  300. return status;
  301. }
  302. /* TODO (#1#): Check for HTTP protocol in URL */
  303. /* Set hostname */
  304. httpc_set_header(conn, HEADER_HOST, url.host);
  305. ssl = url.protocol == PROTOCOL_HTTPS ? 1 : 0;
  306. /* Open connection */
  307. if ((status = hsocket_open(&conn->sock, url.host, url.port, ssl)) != H_OK)
  308. return status;
  309. switch(method)
  310. {
  311. case HTTP_REQUEST_GET:
  312. sprintf(buffer, "GET %s HTTP/%s\r\n",
  313. (url.context[0] != '\0') ? url.context : ("/"),
  314. (conn->version == HTTP_1_0) ? "1.0" : "1.1");
  315. break;
  316. case HTTP_REQUEST_POST:
  317. sprintf(buffer, "POST %s HTTP/%s\r\n",
  318. (url.context[0] != '\0') ? url.context : ("/"),
  319. (conn->version == HTTP_1_0) ? "1.0" : "1.1");
  320. break;
  321. default:
  322. log_error1("Unknown method type!");
  323. return herror_new("httpc_talk_to_server",
  324. GENERAL_INVALID_PARAM,
  325. "hreq_method_t must be HTTP_REQUEST_GET or HTTP_REQUEST_POST");
  326. }
  327. log_verbose1("Sending request...");
  328. if ((status = hsocket_send(&(conn->sock), buffer)) != H_OK)
  329. {
  330. log_error2("Cannot send request (%s)", herror_message(status));
  331. hsocket_close(&(conn->sock));
  332. return status;
  333. }
  334. log_verbose1("Sending header...");
  335. if ((status = httpc_send_header(conn)) != H_OK)
  336. {
  337. log_error2("Cannot send header (%s)", herror_message(status));
  338. hsocket_close(&(conn->sock));
  339. return status;
  340. }
  341. return H_OK;
  342. }
  343. /*--------------------------------------------------
  344. FUNCTION: httpc_get
  345. DESC:
  346. ----------------------------------------------------*/
  347. herror_t
  348. httpc_get(httpc_conn_t * conn, hresponse_t ** out, const char *urlstr)
  349. {
  350. herror_t status;
  351. if ((status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr)) != H_OK)
  352. return status;
  353. if ((status = hresponse_new_from_socket(&(conn->sock), out)) != H_OK)
  354. return status;
  355. return H_OK;
  356. }
  357. /*--------------------------------------------------
  358. FUNCTION: httpc_post_begin
  359. DESC: Returns H_OK if success
  360. ----------------------------------------------------*/
  361. herror_t
  362. httpc_post_begin(httpc_conn_t * conn, const char *url)
  363. {
  364. herror_t status;
  365. if ((status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, url)) != H_OK)
  366. return status;
  367. conn->out = http_output_stream_new(&(conn->sock), conn->header);
  368. return H_OK;
  369. }
  370. /*--------------------------------------------------
  371. FUNCTION: httpc_post_begin
  372. DESC: End a "POST" method and receive the response.
  373. You MUST call httpc_post_end() before!
  374. ----------------------------------------------------*/
  375. herror_t
  376. httpc_post_end(httpc_conn_t * conn, hresponse_t ** out)
  377. {
  378. herror_t status;
  379. if ((status = http_output_stream_flush(conn->out)) != H_OK)
  380. return status;
  381. if ((status = hresponse_new_from_socket(&(conn->sock), out)) != H_OK)
  382. return status;
  383. return H_OK;
  384. }
  385. /* ---------------------------------------------------
  386. MIME support functions httpc_mime_* function set
  387. -----------------------------------------------------*/
  388. static void
  389. _httpc_mime_get_boundary(httpc_conn_t * conn, char *dest)
  390. {
  391. sprintf(dest, "---=.Part_NH_%d", conn->id);
  392. log_verbose2("boundary= \"%s\"", dest);
  393. }
  394. herror_t
  395. httpc_mime_begin(httpc_conn_t * conn, const char *url,
  396. const char *related_start,
  397. const char *related_start_info, const char *related_type)
  398. {
  399. herror_t status;
  400. char buffer[300];
  401. char temp[75];
  402. char boundary[75];
  403. /*
  404. Set Content-type Set multipart/related parameter type=..; start=.. ;
  405. start-info= ..; boundary=...
  406. */
  407. sprintf(buffer, "multipart/related;");
  408. /*
  409. using sprintf instead of snprintf because visual c does not support
  410. snprintf */
  411. #ifdef WIN32
  412. #define snprintf(buffer, num, s1, s2) sprintf(buffer, s1,s2)
  413. #endif
  414. if (related_type)
  415. {
  416. snprintf(temp, 75, " type=\"%s\";", related_type);
  417. strcat(buffer, temp);
  418. }
  419. if (related_start)
  420. {
  421. snprintf(temp, 75, " start=\"%s\";", related_start);
  422. strcat(buffer, temp);
  423. }
  424. if (related_start_info)
  425. {
  426. snprintf(temp, 75, " start-info=\"%s\";", related_start_info);
  427. strcat(buffer, temp);
  428. }
  429. _httpc_mime_get_boundary(conn, boundary);
  430. snprintf(temp, 75, " boundary=\"%s\"", boundary);
  431. strcat(buffer, temp);
  432. httpc_set_header(conn, HEADER_CONTENT_TYPE, buffer);
  433. status = httpc_post_begin(conn, url);
  434. return status;
  435. }
  436. herror_t
  437. httpc_mime_next(httpc_conn_t * conn,
  438. const char *content_id,
  439. const char *content_type, const char *transfer_encoding)
  440. {
  441. herror_t status;
  442. char buffer[512];
  443. char boundary[75];
  444. /* Get the boundary string */
  445. _httpc_mime_get_boundary(conn, boundary);
  446. sprintf(buffer, "\r\n--%s\r\n", boundary);
  447. /* Send boundary */
  448. status = http_output_stream_write(conn->out,
  449. (const byte_t *) buffer, strlen(buffer));
  450. if (status != H_OK)
  451. return status;
  452. /* Send Content header */
  453. sprintf(buffer, "%s: %s\r\n%s: %s\r\n%s: %s\r\n\r\n",
  454. HEADER_CONTENT_TYPE, content_type,
  455. HEADER_CONTENT_TRANSFER_ENCODING, transfer_encoding,
  456. HEADER_CONTENT_ID, content_id);
  457. return http_output_stream_write(conn->out,
  458. (const byte_t *) buffer, strlen(buffer));
  459. }
  460. herror_t
  461. httpc_mime_end(httpc_conn_t * conn, hresponse_t ** out)
  462. {
  463. herror_t status;
  464. char buffer[512];
  465. char boundary[75];
  466. /* Get the boundary string */
  467. _httpc_mime_get_boundary(conn, boundary);
  468. sprintf(buffer, "\r\n--%s--\r\n\r\n", boundary);
  469. /* Send boundary */
  470. status = http_output_stream_write(conn->out,
  471. (const byte_t *) buffer, strlen(buffer));
  472. if (status != H_OK)
  473. return status;
  474. if ((status = http_output_stream_flush(conn->out)) != H_OK)
  475. return status;
  476. if ((status = hresponse_new_from_socket(&(conn->sock), out)) != H_OK)
  477. return status;
  478. return H_OK;
  479. }
  480. /**
  481. Send boundary and part header and continue
  482. with next part
  483. */
  484. herror_t
  485. httpc_mime_send_file(httpc_conn_t * conn,
  486. const char *content_id,
  487. const char *content_type,
  488. const char *transfer_encoding, const char *filename)
  489. {
  490. herror_t status;
  491. FILE *fd = fopen(filename, "rb");
  492. byte_t buffer[MAX_FILE_BUFFER_SIZE];
  493. size_t size;
  494. if (fd == NULL)
  495. return herror_new("httpc_mime_send_file", FILE_ERROR_OPEN,
  496. "Can not open file '%s'", filename);
  497. status = httpc_mime_next(conn, content_id, content_type, transfer_encoding);
  498. if (status != H_OK)
  499. {
  500. fclose(fd);
  501. return status;
  502. }
  503. while (!feof(fd))
  504. {
  505. size = fread(buffer, 1, MAX_FILE_BUFFER_SIZE, fd);
  506. if (size == -1)
  507. {
  508. fclose(fd);
  509. return herror_new("httpc_mime_send_file", FILE_ERROR_READ,
  510. "Can not read from file '%s'", filename);
  511. }
  512. if (size > 0)
  513. {
  514. /* DEBUG: fwrite(buffer, 1, size, stdout); */
  515. status = http_output_stream_write(conn->out, buffer, size);
  516. if (status != H_OK)
  517. {
  518. fclose(fd);
  519. return status;
  520. }
  521. }
  522. }
  523. fclose(fd);
  524. log_verbose1("file sent!");
  525. return H_OK;
  526. }