http2def.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef HV_HTTP2_DEF_H_
  2. #define HV_HTTP2_DEF_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #define HTTP2_MAGIC "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  7. #define HTTP2_MAGIC_LEN 24
  8. // length:3bytes + type:1byte + flags:1byte + stream_id:4bytes = 9bytes
  9. #define HTTP2_FRAME_HDLEN 9
  10. #define HTTP2_UPGRADE_RESPONSE \
  11. "HTTP/1.1 101 Switching Protocols\r\n"\
  12. "Connection: Upgrade\r\n"\
  13. "Upgrade: h2c\r\n\r\n"
  14. typedef enum {
  15. HTTP2_DATA = 0,
  16. HTTP2_HEADERS = 0x01,
  17. HTTP2_PRIORITY = 0x02,
  18. HTTP2_RST_STREAM = 0x03,
  19. HTTP2_SETTINGS = 0x04,
  20. HTTP2_PUSH_PROMISE = 0x05,
  21. HTTP2_PING = 0x06,
  22. HTTP2_GOAWAY = 0x07,
  23. HTTP2_WINDOW_UPDATE = 0x08,
  24. HTTP2_CONTINUATION = 0x09,
  25. HTTP2_ALTSVC = 0x0a,
  26. HTTP2_ORIGIN = 0x0c
  27. } http2_frame_type;
  28. typedef enum {
  29. HTTP2_FLAG_NONE = 0,
  30. HTTP2_FLAG_END_STREAM = 0x01,
  31. HTTP2_FLAG_END_HEADERS = 0x04,
  32. HTTP2_FLAG_PADDED = 0x08,
  33. HTTP2_FLAG_PRIORITY = 0x20
  34. } http2_flag;
  35. typedef struct {
  36. int length;
  37. http2_frame_type type;
  38. http2_flag flags;
  39. int stream_id;
  40. } http2_frame_hd;
  41. static inline void http2_frame_hd_pack(const http2_frame_hd* hd, unsigned char* buf) {
  42. // hton
  43. int length = hd->length;
  44. int stream_id = hd->stream_id;
  45. unsigned char* p = buf;
  46. *p++ = (length >> 16) & 0xFF;
  47. *p++ = (length >> 8) & 0xFF;
  48. *p++ = length & 0xFF;
  49. *p++ = (unsigned char)hd->type;
  50. *p++ = (unsigned char)hd->flags;
  51. *p++ = (stream_id >> 24) & 0xFF;
  52. *p++ = (stream_id >> 16) & 0xFF;
  53. *p++ = (stream_id >> 8) & 0xFF;
  54. *p++ = stream_id & 0xFF;
  55. }
  56. static inline void http2_frame_hd_unpack(const unsigned char* buf, http2_frame_hd* hd) {
  57. // ntoh
  58. const unsigned char* p = buf;
  59. hd->length = *p++ << 16;
  60. hd->length += *p++ << 8;
  61. hd->length += *p++;
  62. hd->type = (http2_frame_type)*p++;
  63. hd->flags = (http2_flag)*p++;
  64. hd->stream_id = *p++ << 24;
  65. hd->stream_id += *p++ << 16;
  66. hd->stream_id += *p++ << 8;
  67. hd->stream_id += *p++;
  68. }
  69. #ifdef __cplusplus
  70. }
  71. #endif
  72. #endif // HV_HTTP2_DEF_H_