httpParser.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. @file httpd.h
  3. @brief Define Constants and fucntions associated with HTTP protocol.
  4. */
  5. #include <stdint.h>
  6. #ifndef __HTTPPARSER_H__
  7. #define __HTTPPARSER_H__
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. //#define _HTTPPARSER_DEBUG_
  12. #define HTTP_SERVER_PORT 80 /**< HTTP server well-known port number */
  13. /* HTTP Method */
  14. #define METHOD_ERR 0 /**< Error Method. */
  15. #define METHOD_GET 1 /**< GET Method. */
  16. #define METHOD_HEAD 2 /**< HEAD Method. */
  17. #define METHOD_POST 3 /**< POST Method. */
  18. /* HTTP GET Method */
  19. #define PTYPE_ERR 0 /**< Error file. */
  20. #define PTYPE_HTML 1 /**< HTML file. */
  21. #define PTYPE_GIF 2 /**< GIF file. */
  22. #define PTYPE_TEXT 3 /**< TEXT file. */
  23. #define PTYPE_JPEG 4 /**< JPEG file. */
  24. #define PTYPE_FLASH 5 /**< FLASH file. */
  25. #define PTYPE_MPEG 6 /**< MPEG file. */
  26. #define PTYPE_PDF 7 /**< PDF file. */
  27. #define PTYPE_CGI 8 /**< CGI file. */
  28. #define PTYPE_XML 9 /**< XML file. */
  29. #define PTYPE_CSS 10 /**< CSS file. */
  30. #define PTYPE_JS 11 /**< JavaScript file. */
  31. #define PTYPE_JSON 12 /**< JSON (JavaScript Standard Object Notation) file. */
  32. #define PTYPE_PNG 13 /**< PNG file. */
  33. #define PTYPE_ICO 14 /**< ICON file. */
  34. #define PTYPE_TTF 20 /**< Font type: TTF file. */
  35. #define PTYPE_OTF 21 /**< Font type: OTF file. */
  36. #define PTYPE_WOFF 22 /**< Font type: WOFF file. */
  37. #define PTYPE_EOT 23 /**< Font type: EOT file. */
  38. #define PTYPE_SVG 24 /**< Font type: SVG file. */
  39. /* HTTP response */
  40. #define STATUS_OK 200
  41. #define STATUS_CREATED 201
  42. #define STATUS_ACCEPTED 202
  43. #define STATUS_NO_CONTENT 204
  44. #define STATUS_MV_PERM 301
  45. #define STATUS_MV_TEMP 302
  46. #define STATUS_NOT_MODIF 304
  47. #define STATUS_BAD_REQ 400
  48. #define STATUS_UNAUTH 401
  49. #define STATUS_FORBIDDEN 403
  50. #define STATUS_NOT_FOUND 404
  51. #define STATUS_INT_SERR 500
  52. #define STATUS_NOT_IMPL 501
  53. #define STATUS_BAD_GATEWAY 502
  54. #define STATUS_SERV_UNAVAIL 503
  55. /* HTML Doc. for ERROR */
  56. static const char ERROR_HTML_PAGE[] = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: 78\r\n\r\n<HTML>\r\n<BODY>\r\nSorry, the page you requested was not found.\r\n</BODY>\r\n</HTML>\r\n\0";
  57. static const char ERROR_REQUEST_PAGE[] = "HTTP/1.1 400 OK\r\nContent-Type: text/html\r\nContent-Length: 50\r\n\r\n<HTML>\r\n<BODY>\r\nInvalid request.\r\n</BODY>\r\n</HTML>\r\n\0";
  58. /* HTML Doc. for CGI result */
  59. #define HTML_HEADER "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: "
  60. /* Response header for HTML*/
  61. #define RES_HTMLHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: keep-alive\r\nContent-Length: "
  62. /* Response head for TEXT */
  63. #define RES_TEXTHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: "
  64. /* Response head for GIF */
  65. #define RES_GIFHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/gif\r\nContent-Length: "
  66. /* Response head for JPEG */
  67. #define RES_JPEGHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\nContent-Length: "
  68. /* Response head for PNG */
  69. #define RES_PNGHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: "
  70. /* Response head for FLASH */
  71. #define RES_FLASHHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/x-shockwave-flash\r\nContent-Length: "
  72. /* Response head for XML */
  73. #define RES_XMLHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/xml\r\nConnection: keep-alive\r\nContent-Length: "
  74. /* Response head for CSS */
  75. #define RES_CSSHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/css\r\nContent-Length: "
  76. /* Response head for JavaScript */
  77. #define RES_JSHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/javascript\r\nContent-Length: "
  78. /* Response head for JSON */
  79. #define RES_JSONHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: "
  80. /* Response head for ICO */
  81. #define RES_ICOHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/x-icon\r\nContent-Length: "
  82. /* Response head for CGI */
  83. #define RES_CGIHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: "
  84. /* Response head for TTF, Font */
  85. #define RES_TTFHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/x-font-truetype\r\nContent-Length: "
  86. /* Response head for OTF, Font */
  87. #define RES_OTFHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/x-font-opentype\r\nContent-Length: "
  88. /* Response head for WOFF, Font */
  89. #define RES_WOFFHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/font-woff\r\nContent-Length: "
  90. /* Response head for EOT, Font */
  91. #define RES_EOTHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.ms-fontobject\r\nContent-Length: "
  92. /* Response head for SVG, Font */
  93. #define RES_SVGHEAD_OK "HTTP/1.1 200 OK\r\nContent-Type: image/svg+xml\r\nContent-Length: "
  94. /**
  95. @brief Structure of HTTP REQUEST
  96. */
  97. //#define MAX_URI_SIZE 1461
  98. #define MAX_URI_SIZE 512
  99. typedef struct _st_http_request
  100. {
  101. uint8_t METHOD; /**< request method(METHOD_GET...). */
  102. uint8_t TYPE; /**< request type(PTYPE_HTML...). */
  103. uint8_t URI[MAX_URI_SIZE]; /**< request file name. */
  104. }st_http_request;
  105. // HTTP Parsing functions
  106. void unescape_http_url(char * url); /* convert escape character to ascii */
  107. void parse_http_request(st_http_request *, uint8_t *); /* parse request from peer */
  108. void find_http_uri_type(uint8_t *, uint8_t *); /* find MIME type of a file */
  109. void make_http_response_head(char *, char, uint32_t); /* make response header */
  110. uint8_t * get_http_param_value(char* uri, char* param_name); /* get the user-specific parameter value */
  111. uint8_t get_http_uri_name(uint8_t * uri, uint8_t * uri_buf); /* get the requested URI name */
  112. #ifdef _OLD_
  113. uint8_t * get_http_uri_name(uint8_t * uri);
  114. #endif
  115. // Utility functions
  116. uint16_t ATOI(uint8_t * str, uint8_t base);
  117. void mid(char* src, char* s1, char* s2, char* sub);
  118. void inet_addr_(uint8_t * addr, uint8_t * ip);
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif /* end of __HTTPPARSER_H__ */