nghttp2_outbound_item.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 Tatsuhiro Tsujikawa
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGHTTP2_OUTBOUND_ITEM_H
  26. #define NGHTTP2_OUTBOUND_ITEM_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* HAVE_CONFIG_H */
  30. #include <nghttp2/nghttp2.h>
  31. #include "nghttp2_frame.h"
  32. #include "nghttp2_mem.h"
  33. #define NGHTTP2_DATA_PROVIDER_V1 1
  34. #define NGHTTP2_DATA_PROVIDER_V2 2
  35. typedef struct nghttp2_data_provider_wrap {
  36. int version;
  37. union {
  38. struct {
  39. nghttp2_data_source source;
  40. void *read_callback;
  41. };
  42. nghttp2_data_provider v1;
  43. nghttp2_data_provider2 v2;
  44. } data_prd;
  45. } nghttp2_data_provider_wrap;
  46. nghttp2_data_provider_wrap *
  47. nghttp2_data_provider_wrap_v1(nghttp2_data_provider_wrap *dpw,
  48. const nghttp2_data_provider *data_prd);
  49. nghttp2_data_provider_wrap *
  50. nghttp2_data_provider_wrap_v2(nghttp2_data_provider_wrap *dpw,
  51. const nghttp2_data_provider2 *data_prd);
  52. /* struct used for HEADERS and PUSH_PROMISE frame */
  53. typedef struct {
  54. nghttp2_data_provider_wrap dpw;
  55. void *stream_user_data;
  56. /* error code when request HEADERS is canceled by RST_STREAM while
  57. it is in queue. */
  58. uint32_t error_code;
  59. /* nonzero if request HEADERS is canceled. The error code is stored
  60. in |error_code|. */
  61. uint8_t canceled;
  62. } nghttp2_headers_aux_data;
  63. /* struct used for DATA frame */
  64. typedef struct {
  65. /**
  66. * The data to be sent for this DATA frame.
  67. */
  68. nghttp2_data_provider_wrap dpw;
  69. /**
  70. * The flags of DATA frame. We use separate flags here and
  71. * nghttp2_data frame. The latter contains flags actually sent to
  72. * peer. This |flags| may contain NGHTTP2_FLAG_END_STREAM and only
  73. * when |eof| becomes nonzero, flags in nghttp2_data has
  74. * NGHTTP2_FLAG_END_STREAM set.
  75. */
  76. uint8_t flags;
  77. /**
  78. * The flag to indicate whether EOF was reached or not. Initially
  79. * |eof| is 0. It becomes 1 after all data were read.
  80. */
  81. uint8_t eof;
  82. /**
  83. * The flag to indicate that NGHTTP2_DATA_FLAG_NO_COPY is used.
  84. */
  85. uint8_t no_copy;
  86. } nghttp2_data_aux_data;
  87. typedef enum {
  88. NGHTTP2_GOAWAY_AUX_NONE = 0x0,
  89. /* indicates that session should be terminated after the
  90. transmission of this frame. */
  91. NGHTTP2_GOAWAY_AUX_TERM_ON_SEND = 0x1,
  92. /* indicates that this GOAWAY is just a notification for graceful
  93. shutdown. No nghttp2_session.goaway_flags should be updated on
  94. the reaction to this frame. */
  95. NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE = 0x2
  96. } nghttp2_goaway_aux_flag;
  97. /* struct used for GOAWAY frame */
  98. typedef struct {
  99. /* bitwise-OR of one or more of nghttp2_goaway_aux_flag. */
  100. uint8_t flags;
  101. } nghttp2_goaway_aux_data;
  102. /* struct used for extension frame */
  103. typedef struct {
  104. /* nonzero if this extension frame is serialized by library
  105. function, instead of user-defined callbacks. */
  106. uint8_t builtin;
  107. } nghttp2_ext_aux_data;
  108. /* Additional data which cannot be stored in nghttp2_frame struct */
  109. typedef union {
  110. nghttp2_data_aux_data data;
  111. nghttp2_headers_aux_data headers;
  112. nghttp2_goaway_aux_data goaway;
  113. nghttp2_ext_aux_data ext;
  114. } nghttp2_aux_data;
  115. struct nghttp2_outbound_item;
  116. typedef struct nghttp2_outbound_item nghttp2_outbound_item;
  117. struct nghttp2_outbound_item {
  118. nghttp2_frame frame;
  119. /* Storage for extension frame payload. frame->ext.payload points
  120. to this structure to avoid frequent memory allocation. */
  121. nghttp2_ext_frame_payload ext_frame_payload;
  122. nghttp2_aux_data aux_data;
  123. /* The priority used in priority comparison. Smaller is served
  124. earlier. For PING, SETTINGS and non-DATA frames (excluding
  125. response HEADERS frame) have dedicated cycle value defined above.
  126. For DATA frame, cycle is computed by taking into account of
  127. effective weight and frame payload length previously sent, so
  128. that the amount of transmission is distributed across streams
  129. proportional to effective weight (inside a tree). */
  130. uint64_t cycle;
  131. nghttp2_outbound_item *qnext;
  132. /* nonzero if this object is queued, except for DATA or HEADERS
  133. which are attached to stream as item. */
  134. uint8_t queued;
  135. };
  136. /*
  137. * Initializes |item|. No memory allocation is done in this function.
  138. * Don't call nghttp2_outbound_item_free() until frame member is
  139. * initialized.
  140. */
  141. void nghttp2_outbound_item_init(nghttp2_outbound_item *item);
  142. /*
  143. * Deallocates resource for |item|. If |item| is NULL, this function
  144. * does nothing.
  145. */
  146. void nghttp2_outbound_item_free(nghttp2_outbound_item *item, nghttp2_mem *mem);
  147. /*
  148. * queue for nghttp2_outbound_item.
  149. */
  150. typedef struct {
  151. nghttp2_outbound_item *head, *tail;
  152. /* number of items in this queue. */
  153. size_t n;
  154. } nghttp2_outbound_queue;
  155. void nghttp2_outbound_queue_init(nghttp2_outbound_queue *q);
  156. /* Pushes |item| into |q| */
  157. void nghttp2_outbound_queue_push(nghttp2_outbound_queue *q,
  158. nghttp2_outbound_item *item);
  159. /* Pops |item| at the top from |q|. If |q| is empty, nothing
  160. happens. */
  161. void nghttp2_outbound_queue_pop(nghttp2_outbound_queue *q);
  162. /* Returns the top item. */
  163. #define nghttp2_outbound_queue_top(Q) ((Q)->head)
  164. /* Returns the size of the queue */
  165. #define nghttp2_outbound_queue_size(Q) ((Q)->n)
  166. #endif /* NGHTTP2_OUTBOUND_ITEM_H */