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.

144 lines
3.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: http_client.c,v 1.2 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: A http client/server library in C (example)
  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: hero@persua.de
  23. ******************************************************************/
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <nanohttp/nanohttp-logging.h>
  27. #include <nanohttp/nanohttp-client.h>
  28. #define MAX_BUFFER_SIZE 1024
  29. static int show_http_status_code = 0;
  30. static int show_headers = 0;
  31. static int show_content = 1;
  32. static char *username = NULL;
  33. static char *password = NULL;
  34. static void show_response(hresponse_t *res) {
  35. byte_t buffer[MAX_BUFFER_SIZE+1];
  36. int read;
  37. if (!res) {
  38. printf("hresponse_t is NULL!");
  39. return;
  40. }
  41. if (res->errcode != 200 || show_http_status_code)
  42. printf("HTTP Status: %d \"%s\"\n", res->errcode, res->desc);
  43. if (show_headers) {
  44. hpair_t *pair;
  45. printf("HTTP Headers:\n");
  46. for (pair = res->header; pair; pair=pair->next)
  47. printf(" %s: %s\n", pair->key, pair->value);
  48. }
  49. if (!res->in) {
  50. log_warn1("No input stream!");
  51. return;
  52. }
  53. while (http_input_stream_is_ready(res->in)) {
  54. read = http_input_stream_read(res->in, buffer, MAX_BUFFER_SIZE);
  55. if (show_content)
  56. fwrite(buffer, read, 1, stdout);
  57. }
  58. return;
  59. }
  60. int main(int argc, char **argv) {
  61. httpc_conn_t *conn; /* Client connection object */
  62. hresponse_t *res; /* Response object **/
  63. herror_t status;
  64. int i;
  65. /* check usage */
  66. if (argc < 2) {
  67. fprintf(stderr, "usage: %s [-headers] [-status] [-noout] [-username name] [-password secret] <url>\n", argv[0]);
  68. exit(1);
  69. }
  70. /* XXX: this is not safe... */
  71. for (i=0; i<argc; i++) {
  72. if (!strcmp("-headers", argv[i]))
  73. show_headers = 1;
  74. else if (!strcmp("-status", argv[i]))
  75. show_http_status_code = 1;
  76. else if (!strcmp("-noout", argv[i]))
  77. show_content = 0;
  78. else if (!strcmp("-username", argv[i]))
  79. username = argv[i+1];
  80. else if (!strcmp("-password", argv[i]))
  81. password = argv[i+1];
  82. }
  83. /* Set log level to see more information written by the library */
  84. hlog_set_level(HLOG_INFO);
  85. /* Initialize httpc module */
  86. if (httpc_init(argc, argv)) {
  87. log_error1("Can not init httpc");
  88. exit(1);
  89. }
  90. /* Create the client connection object */
  91. if (!(conn = httpc_new())) {
  92. log_error1("httpc_new failed");
  93. exit(1);
  94. }
  95. /* set the credentials, if specified */
  96. if (username || password)
  97. httpc_set_basic_authorization(conn, username, password);
  98. /* Send GET method and receive response */
  99. if ((status = httpc_get(conn, &res, argv[argc-1])) != H_OK) {
  100. log_error2("httpc_get failed (%s)", herror_message(status));
  101. herror_release(status);
  102. exit(1);
  103. }
  104. /* Show response */
  105. show_response(res);
  106. /* Clean up */
  107. hresponse_free(res);
  108. httpc_free(conn);
  109. return 0;
  110. }