Guest User

Untitled

a guest
Jan 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <sys/stat.h>
  6. #include <event.h>
  7. #include <evhttp.h>
  8.  
  9. #define HTTPD_ADDR "0.0.0.0"
  10. #define HTTPD_PORT 8080
  11. #define DOCUMENT_ROOT "."
  12.  
  13. static const char *get_mime_type(const char *filepath)
  14. {
  15. const char *filetype = strrchr(filepath, '.');
  16.  
  17. if (strcasecmp(filetype, ".html") == 0 ||
  18. strcasecmp(filetype, ".htm") == 0)
  19. return "text/html";
  20. else if (strcasecmp(filetype, ".js") == 0)
  21. return "text/javascript";
  22. else if (strcasecmp(filetype, ".css") == 0)
  23. return "text/css";
  24. else if (strcasecmp(filetype, ".txt") == 0)
  25. return "text/plain";
  26. else if (strcasecmp(filetype, ".ico") == 0)
  27. return "image/x-icon";
  28. else if (strcasecmp(filetype, ".png") == 0)
  29. return "image/png";
  30. else if (strcasecmp(filetype, ".gif") == 0)
  31. return "image/gif";
  32. else if (strcasecmp(filetype, ".jpeg") == 0 ||
  33. strcasecmp(filetype, ".jpg") == 0)
  34. return "image/jpeg";
  35. return "application/ocet-stream";
  36. }
  37.  
  38. static const char *get_content_from_file(const char *req_path, char **content, int *content_length)
  39. {
  40. struct stat sb;
  41. static char filepath[1024];
  42. FILE *fp;
  43. int rsize;
  44.  
  45. *content = NULL;
  46. sprintf(filepath, "%s%s", DOCUMENT_ROOT, req_path);
  47.  
  48. if (stat(filepath, &sb) < 0)
  49. return filepath;
  50.  
  51. if (S_ISDIR(sb.st_mode)) {
  52. if (strcmp(req_path, "/") == 0)
  53. sprintf(filepath, "%s%sindex.html", DOCUMENT_ROOT, req_path);
  54. else
  55. sprintf(filepath, "%s%s/index.html", DOCUMENT_ROOT, req_path);
  56.  
  57. if (stat(filepath, &sb) < 0)
  58. return filepath;
  59. }
  60.  
  61. *content_length = (int)sb.st_size;
  62.  
  63. fp = fopen(filepath, "rb");
  64. if (fp) {
  65. *content = (char *)malloc(*content_length);
  66. rsize = fread(*content, 1, *content_length, fp);
  67. fclose(fp);
  68. if (rsize != *content_length) {
  69. free(*content);
  70. *content = NULL;
  71. }
  72. }
  73.  
  74. return filepath;
  75. }
  76.  
  77. void req_handler(struct evhttp_request *r, void *arg)
  78. {
  79. struct evbuffer *evbuf;
  80. const char *req_path, *res_path;
  81. char *content;
  82. int content_length;
  83. char content_length_str[12];
  84.  
  85. if (r->type != EVHTTP_REQ_GET) {
  86. evhttp_send_error(r, HTTP_BADREQUEST, "available GET only");
  87. return;
  88. }
  89.  
  90. req_path = evhttp_request_uri(r);
  91. res_path = get_content_from_file(req_path, &content, &content_length);
  92. if (content == NULL) {
  93. evhttp_send_error(r, HTTP_NOTFOUND, "Not Found");
  94. return;
  95. }
  96. sprintf(content_length_str, "%d", content_length);
  97.  
  98. evhttp_add_header(r->output_headers, "Content-Type", get_mime_type(res_path));
  99. evhttp_add_header(r->output_headers, "Content-Length", content_length_str);
  100.  
  101. evbuf = evbuffer_new();
  102. if (evbuf == NULL) {
  103. evhttp_send_error(r, HTTP_SERVUNAVAIL, "failed to create buffer");
  104. free(content);
  105. return;
  106. }
  107. evbuffer_add(evbuf, content, content_length);
  108. evhttp_send_reply(r, HTTP_OK, "", evbuf);
  109. evbuffer_free(evbuf);
  110. }
  111.  
  112. int main(int argc, const char* argv[])
  113. {
  114. struct event_base *ev_base;
  115. struct evhttp *httpd;
  116.  
  117. ev_base = event_base_new();
  118.  
  119. httpd = evhttp_new(ev_base);
  120. if (evhttp_bind_socket(httpd, HTTPD_ADDR, HTTPD_PORT) < 0) {
  121. perror("evhttp_bind_socket()");
  122. exit(EXIT_FAILURE);
  123. }
  124. evhttp_set_gencb(httpd, req_handler, NULL);
  125.  
  126. event_base_dispatch(ev_base);
  127.  
  128. evhttp_free(httpd);
  129. event_base_free(ev_base);
  130.  
  131. return 0;
  132. }
Add Comment
Please, Sign In to add comment