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.

186 lines
4.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: http_server.c,v 1.4 2006/07/09 16:24:19 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: hero@persua.de
  23. ******************************************************************/
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <nanohttp/nanohttp-logging.h>
  27. #include <nanohttp/nanohttp-server.h>
  28. static int simple_authenticator(hrequest_t *req, const char *user, const char *password)
  29. {
  30. log_info3("logging in user=\"%s\" password=\"%s\"", user, password);
  31. if (strcmp(user, "bob")) {
  32. log_warn2("user \"%s\" unkown", user);
  33. return 0;
  34. }
  35. if (strcmp(password, "builder")) {
  36. log_warn1("wrong password");
  37. return 0;
  38. }
  39. return 1;
  40. }
  41. static void secure_service(httpd_conn_t *conn, hrequest_t *req)
  42. {
  43. httpd_send_header(conn, 200, "OK");
  44. hsocket_send(conn->sock,
  45. "<html>"
  46. "<head>"
  47. "<title>Secure ressource!</title>"
  48. "</head>"
  49. "<body>"
  50. "<h1>Authenticated access!!!</h1>"
  51. "</body>"
  52. "</html>");
  53. return;
  54. }
  55. static void default_service(httpd_conn_t *conn, hrequest_t *req)
  56. {
  57. httpd_send_header(conn, 404, "Not found");
  58. hsocket_send(conn->sock,
  59. "<html>"
  60. "<head>"
  61. "<title>Default error page</title>"
  62. "</head>"
  63. "<body>"
  64. "<h1>Default error page</h1>"
  65. "<div>");
  66. hsocket_send(conn->sock, req->path);
  67. hsocket_send(conn->sock, " can not be found"
  68. "</div>"
  69. "</body>"
  70. "</html>");
  71. return;
  72. }
  73. static void headers_service(httpd_conn_t *conn, hrequest_t *req)
  74. {
  75. hpair_t *walker;
  76. httpd_send_header(conn, 200, "OK");
  77. hsocket_send(conn->sock,
  78. "<html>"
  79. "<head>"
  80. "<title>Request headers</title>"
  81. "</head>"
  82. "<body>"
  83. "<h1>Request headers</h1>"
  84. "<ul>");
  85. for (walker=req->header; walker; walker=walker->next)
  86. {
  87. hsocket_send(conn->sock, "<li>");
  88. hsocket_send(conn->sock, walker->key);
  89. hsocket_send(conn->sock, " = ");
  90. hsocket_send(conn->sock, walker->value);
  91. hsocket_send(conn->sock, "</li>");
  92. }
  93. hsocket_send(conn->sock,
  94. "</ul>"
  95. "</body>"
  96. "</html>");
  97. return;
  98. }
  99. static void root_service(httpd_conn_t *conn, hrequest_t *req)
  100. {
  101. httpd_send_header(conn, 200, "OK");
  102. hsocket_send(conn->sock,
  103. "<html>"
  104. "<head>"
  105. "<title>nanoHTTP server examples</title>"
  106. "</head>"
  107. "<body>"
  108. "<h1>nanoHTTP server examples</h1>"
  109. "<ul>"
  110. "<li><a href=\"/\">Simple service</a></li>"
  111. "<li><a href=\"/secure\">Secure service</a> (try: bob/builder)</li>"
  112. "<li><a href=\"/headers\">Request headers</a></li>"
  113. "<li><a href=\"/not_existent\">The default service</a></li>"
  114. "</ul>"
  115. "</body>"
  116. "</html>");
  117. return;
  118. }
  119. int main(int argc, char *argv[])
  120. {
  121. hlog_set_level(HLOG_INFO);
  122. if (httpd_init(argc, argv)) {
  123. fprintf(stderr, "Can not init httpd");
  124. return 1;
  125. }
  126. if (!httpd_register("/", root_service)) {
  127. fprintf(stderr, "Can not register service");
  128. return 1;
  129. }
  130. if (!httpd_register_secure("/secure", secure_service, simple_authenticator)) {
  131. fprintf(stderr, "Can not register secure service");
  132. return 1;
  133. }
  134. if (!httpd_register("/headers", headers_service)) {
  135. fprintf(stderr, "Can not register headers service");
  136. return 1;
  137. }
  138. if (!httpd_register_default("/error", default_service)) {
  139. fprintf(stderr, "Can not register default service");
  140. return 1;
  141. }
  142. if (httpd_run()) {
  143. fprintf(stderr, "can not run httpd");
  144. return 1;
  145. }
  146. httpd_destroy();
  147. return 0;
  148. }