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.

150 lines
3.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: client_post_chunked.c,v 1.1 2004/10/15 13:42:07 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C (example)
  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. #include <nanohttp/nanohttp-client.h>
  25. #include <stdio.h>
  26. #define MAX_BUFFER_SIZE 1024
  27. static
  28. void show_response(hresponse_t *res)
  29. {
  30. hpair_t *pair;
  31. byte_t buffer[MAX_BUFFER_SIZE+1];
  32. int read;
  33. if (res == NULL)
  34. {
  35. log_error1("Response is NULL!");
  36. return;
  37. }
  38. log_info2("Spec : '%s'", res->spec);
  39. log_info2("Status: %d", res->errcode);
  40. log_info2("Desc : '%s'", res->desc);
  41. pair = res->header;
  42. while (pair != NULL) {
  43. log_debug3("%s: %s", pair->key, pair->value);
  44. pair = pair->next;
  45. }
  46. if (res->in == NULL)
  47. {
  48. log_warn1("No input stream!");
  49. return;
  50. }
  51. while (http_input_stream_is_ready(res->in))
  52. {
  53. read = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE);
  54. buffer[read] = '\0';
  55. puts(buffer);
  56. }
  57. }
  58. int main(int argc, char *argv[])
  59. {
  60. httpc_conn_t *conn; /* Client connection object */
  61. hresponse_t *res; /* Response object **/
  62. char postdata[1054];
  63. /* Check usage */
  64. if (argc < 2) {
  65. fprintf(stderr, "usage %s <url> \n", argv[0]);
  66. exit(1);
  67. }
  68. /* Set log level to see more information written by the library */
  69. log_set_level(HLOG_VERBOSE);
  70. /* Initialize httpc module */
  71. if (httpc_init(argc, argv))
  72. {
  73. log_error1("Can not init httpc");
  74. return 1;
  75. }
  76. /* Create the client connection object */
  77. conn = httpc_new();
  78. /* Set header for chunked transport */
  79. httpc_set_header(conn, HEADER_TRANSFER_ENCODING, TRANSFER_ENCODING_CHUNKED);
  80. /* POSTing will be done in 3 steps
  81. 1. httpc_post_begin()
  82. 2. http_output_stream_write()
  83. 3. httpc_post_end()
  84. */
  85. /* (1) First step begin the post request*/
  86. if (httpc_post_begin(conn, argv[1]) != HSOCKET_OK)
  87. {
  88. log_error1("Can not begin posting\n");
  89. httpc_free(conn);
  90. exit(1);
  91. }
  92. /* (2) Post some data*/
  93. while (1)
  94. {
  95. printf("\nEnter post data (max 1054). [Type '.' to end]: ");
  96. gets(postdata);
  97. if (!strcmp(postdata, "."))
  98. break;
  99. else if (http_output_stream_write(conn->out, postdata, strlen(postdata))
  100. != HSOCKET_OK )
  101. {
  102. log_error1("Can not post\n");
  103. httpc_free(conn);
  104. exit(1);
  105. }
  106. }
  107. /* (3) End POSTing data and receive the response */
  108. printf("\nReceiving ... \n ");
  109. res = httpc_post_end(conn);
  110. if (res == NULL)
  111. {
  112. log_error2("Error: %s", conn->errmsg);
  113. httpc_free(conn);
  114. exit(1);
  115. }
  116. show_response(res);
  117. /* Clean up */
  118. hresponse_free(res);
  119. httpc_free(conn);
  120. return 0;
  121. }