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.8KB

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