libfuncs is collection of code (list, queue, circular buffer, io, logging, etc.). https://georgi.unixsol.org/programs/libfuncs/
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.

http_response.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * HTTP responses
  3. * Copyright (C) 2006-2009 Unix Solutions Ltd.
  4. *
  5. * Released under MIT license.
  6. * See LICENSE-MIT.txt for license terms.
  7. */
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. #include <netdb.h>
  11. #include <time.h>
  12. #include "io.h"
  13. static char *server_signature = NULL;
  14. static char *server_version = NULL;
  15. void set_http_response_server_ident(char *server, char *version) {
  16. server_signature = server;
  17. server_version = version;
  18. }
  19. void send_http_response(int clientsock, const char *response) {
  20. char buf[128];
  21. time_t now = time(NULL);
  22. fdputsf(clientsock, "HTTP/1.0 %s\n", response);
  23. if (server_signature && server_version)
  24. fdputsf(clientsock, "Server: %s %s\n", server_signature, server_version);
  25. strftime(buf,sizeof(buf),"Date: %a, %d %b %Y %H:%M:%S %Z\n",gmtime(&now));
  26. fdputs(clientsock,buf);
  27. fdputs(clientsock, "Cache-Control: no-cache\n");
  28. fdputs(clientsock, "Connection: close\n");
  29. fdputs(clientsock, "Pragma: no-cache\n");
  30. }
  31. void send_header_textplain(int clientsock) {
  32. fdputs(clientsock,"Content-Type: text/plain\n");
  33. }
  34. void send_http_error(int clientsock, const char *code, const char *message) {
  35. send_http_response(clientsock, code);
  36. send_header_textplain(clientsock);
  37. if (message) {
  38. fdputsf(clientsock,"X-Error: %s\n", message);
  39. }
  40. fdputs(clientsock,"\n");
  41. fdputsf(clientsock,"%s\n", message ? message : code);
  42. }
  43. void send_200_ok(int clientsock) {
  44. send_http_response(clientsock,"200 OK");
  45. }
  46. void send_http_ok(int clientsock, char *message) {
  47. send_200_ok(clientsock);
  48. send_header_textplain(clientsock);
  49. fdputs(clientsock, "\n");
  50. fdputs(clientsock, message);
  51. fdputs(clientsock, "\n");
  52. }
  53. void send_http_ok_msg(int clientsock, const char *fmt, ...) {
  54. char msg[512];
  55. va_list args;
  56. va_start(args, fmt);
  57. vsnprintf(msg, 512, fmt, args);
  58. va_end(args);
  59. send_http_ok(clientsock, msg);
  60. }
  61. void send_302_redirect(int clientsock, char * url) {
  62. send_http_response(clientsock,"302 Found");
  63. fdputsf(clientsock,"Location: %s\n", url);
  64. fdputs(clientsock,"\n");
  65. }
  66. void send_400_bad_request(int clientsock, const char * msg) {
  67. send_http_error(clientsock,"400 Bad Request", msg);
  68. }
  69. void send_403_forbidden_msg(int clientsock, const char *msg) {
  70. send_http_error(clientsock, "403 Forbidden", msg);
  71. }
  72. void send_403_forbidden(int clientsock) {
  73. send_403_forbidden_msg(clientsock, "access-denied");
  74. }
  75. void send_404_not_found(int clientsock) {
  76. send_http_error(clientsock, "404 Not Found", NULL);
  77. }
  78. void send_409_conflict(int clientsock, const char * msg) {
  79. send_http_error(clientsock, "409 Conflict", msg);
  80. }
  81. void send_500_internal_server_error(int clientsock, const char * msg) {
  82. send_http_error(clientsock, "500 Internal Server Error", msg);
  83. }
  84. void send_501_not_implemented(int clientsock) {
  85. send_http_error(clientsock, "501 Method Not Implemented", NULL);
  86. }
  87. void send_504_gateway_timeout(int clientsock) {
  88. send_http_error(clientsock, "504 Gateway Timeout", "no-signal");
  89. }