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.

213 lines
5.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: postserver.c,v 1.4 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-server.h>
  25. #include <stdio.h>
  26. /*
  27. Please ignore this file. This is just for me to test
  28. some features and new functions. In the future this
  29. file will also be good start to learn nanohttp
  30. */
  31. static
  32. void _print_binary_ascii(int n)
  33. {
  34. int i,c=0;
  35. char ascii[36];
  36. for (i=0;i<32;i++) {
  37. ascii[34-i-c] = (n & (1<<i))?'1':'0';
  38. if ((i+1)%8 == 0) {
  39. c++;
  40. ascii[i+c] = ' ';
  41. }
  42. }
  43. ascii[35]='\0';
  44. log_verbose2("%s", ascii);
  45. }
  46. static
  47. void _print_binary_ascii2(unsigned char n)
  48. {
  49. int i,c=0;
  50. char ascii[9];
  51. for (i=0;i<8;i++) {
  52. ascii[7-i] = (n & (1<<i))?'1':'0';
  53. }
  54. ascii[8]='\0';
  55. log_verbose2("%s", ascii);
  56. }
  57. void echomime_service(httpd_conn_t *conn, hrequest_t *req)
  58. {
  59. part_t *part;
  60. int status;
  61. char *content_type , *transfer_encoding,
  62. *content_id, *root_content_type;
  63. if (!req->attachments || !req->content_type)
  64. {
  65. httpd_send_header(conn, 200, "OK");
  66. http_output_stream_write_string(conn->out, "<html><body><h1>");
  67. http_output_stream_write_string(conn->out,
  68. "You do not posted a MIME message!<hr></h1></body></html>");
  69. return;
  70. }
  71. else
  72. {
  73. root_content_type = hpairnode_get(req->content_type->params, "type");
  74. status =
  75. httpd_mime_send_header(conn, req->attachments->root_part->id, "",
  76. root_content_type, 200, "OK");
  77. if (status != H_OK)
  78. return;
  79. part = req->attachments->parts;
  80. while (part)
  81. {
  82. content_id = hpairnode_get(part->header, HEADER_CONTENT_ID);
  83. content_type = hpairnode_get(part->header, HEADER_CONTENT_TYPE);
  84. transfer_encoding = hpairnode_get(part->header, HEADER_TRANSFER_ENCODING);
  85. status = httpd_mime_send_file(conn, content_id,
  86. content_type, transfer_encoding, part->filename);
  87. if (status != H_OK)
  88. return;
  89. part = part->next;
  90. }
  91. httpd_mime_end(conn);
  92. }
  93. }
  94. /*
  95. SERVICE: http://host:port/postserver
  96. */
  97. void post_service(httpd_conn_t *conn, hrequest_t *req)
  98. {
  99. unsigned char *postdata;
  100. long received, total=0;
  101. unsigned int tmp;
  102. char buffer[15];
  103. hpair_t *pair;
  104. /*
  105. postdata = httpd_get_postdata(conn, req, &received, -1);
  106. */
  107. if (req->method == HTTP_REQUEST_POST) {
  108. httpd_send_header(conn, 200, "OK");
  109. http_output_stream_write_string(conn->out, "<html><body>\n");
  110. http_output_stream_write_string(conn->out, "<h3>You Posted:</h3><hr>\n");
  111. while (http_input_stream_is_ready(req->in))
  112. {
  113. received = http_input_stream_read(req->in, buffer, 10);
  114. http_output_stream_write(conn->out, buffer, received);
  115. total += received;
  116. }
  117. http_output_stream_write_string(conn->out, "<h3>Received size</h3><hr>\n");
  118. sprintf(buffer, "%d", total);
  119. http_output_stream_write_string(conn->out, buffer);
  120. _print_binary_ascii2(postdata[0]);
  121. _print_binary_ascii2(postdata[1]);
  122. _print_binary_ascii2(postdata[2]);
  123. _print_binary_ascii2(postdata[3]);
  124. /* free(postdata);*/
  125. } else {
  126. httpd_send_header(conn, 200, "OK");
  127. http_output_stream_write_string(conn->out, "<html><body>");
  128. http_output_stream_write_string(conn->out, "<form action=\"/postserver\" method=\"POST\">");
  129. http_output_stream_write_string(conn->out, "Enter Postdata: <input name=\"field\" type=\"text\">");
  130. http_output_stream_write_string(conn->out, "<input type=\"submit\">");
  131. }
  132. http_output_stream_write_string(conn->out, "<hr><p><small>Content-Type:");
  133. if (req->content_type)
  134. {
  135. http_output_stream_write_string(conn->out, req->content_type->type);
  136. http_output_stream_write_string(conn->out, "<br>");
  137. pair = req->content_type->params;
  138. while (pair)
  139. {
  140. http_output_stream_write_string(conn->out, pair->key);
  141. http_output_stream_write_string(conn->out, "=");
  142. http_output_stream_write_string(conn->out, pair->value);
  143. http_output_stream_write_string(conn->out, "<br>");
  144. pair = pair->next;
  145. }
  146. }
  147. else
  148. {
  149. http_output_stream_write_string(conn->out, "Not available");
  150. }
  151. http_output_stream_write_string(conn->out, "</body></html>");
  152. }
  153. int main(int argc, char *argv[])
  154. {
  155. log_set_level(HLOG_VERBOSE);
  156. if (httpd_init(argc, argv)) {
  157. fprintf(stderr, "can not init httpd");
  158. return 1;
  159. }
  160. if (!httpd_register("/postserver", post_service)) {
  161. fprintf(stderr, "Can not register service");
  162. return 1;
  163. }
  164. if (!httpd_register("/axis/services/urn:EchoAttachmentsService", echomime_service)) {
  165. fprintf(stderr, "Can not register service");
  166. return 1;
  167. }
  168. if (httpd_run()) {
  169. fprintf(stderr, "can not run httpd");
  170. return 1;
  171. }
  172. httpd_destroy();
  173. #ifdef MEM_DEBUG
  174. _mem_report();
  175. #endif
  176. return 0;
  177. }