nghttp3_frame.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2013 nghttp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include "nghttp3_frame.h"
  27. #include <string.h>
  28. #include <assert.h>
  29. #include "nghttp3_conv.h"
  30. #include "nghttp3_str.h"
  31. uint8_t *nghttp3_frame_write_hd(uint8_t *p, const nghttp3_frame_hd *hd) {
  32. p = nghttp3_put_varint(p, hd->type);
  33. p = nghttp3_put_varint(p, hd->length);
  34. return p;
  35. }
  36. size_t nghttp3_frame_write_hd_len(const nghttp3_frame_hd *hd) {
  37. return nghttp3_put_varintlen(hd->type) + nghttp3_put_varintlen(hd->length);
  38. }
  39. uint8_t *nghttp3_frame_write_settings(uint8_t *p,
  40. const nghttp3_frame_settings *fr) {
  41. size_t i;
  42. p = nghttp3_frame_write_hd(p, &fr->hd);
  43. for (i = 0; i < fr->niv; ++i) {
  44. p = nghttp3_put_varint(p, (int64_t)fr->iv[i].id);
  45. p = nghttp3_put_varint(p, (int64_t)fr->iv[i].value);
  46. }
  47. return p;
  48. }
  49. size_t nghttp3_frame_write_settings_len(int64_t *ppayloadlen,
  50. const nghttp3_frame_settings *fr) {
  51. size_t payloadlen = 0;
  52. size_t i;
  53. for (i = 0; i < fr->niv; ++i) {
  54. payloadlen += nghttp3_put_varintlen((int64_t)fr->iv[i].id) +
  55. nghttp3_put_varintlen((int64_t)fr->iv[i].value);
  56. }
  57. *ppayloadlen = (int64_t)payloadlen;
  58. return nghttp3_put_varintlen(NGHTTP3_FRAME_SETTINGS) +
  59. nghttp3_put_varintlen((int64_t)payloadlen) + payloadlen;
  60. }
  61. uint8_t *nghttp3_frame_write_goaway(uint8_t *p,
  62. const nghttp3_frame_goaway *fr) {
  63. p = nghttp3_frame_write_hd(p, &fr->hd);
  64. p = nghttp3_put_varint(p, fr->id);
  65. return p;
  66. }
  67. size_t nghttp3_frame_write_goaway_len(int64_t *ppayloadlen,
  68. const nghttp3_frame_goaway *fr) {
  69. size_t payloadlen = nghttp3_put_varintlen(fr->id);
  70. *ppayloadlen = (int64_t)payloadlen;
  71. return nghttp3_put_varintlen(NGHTTP3_FRAME_GOAWAY) +
  72. nghttp3_put_varintlen((int64_t)payloadlen) + payloadlen;
  73. }
  74. uint8_t *
  75. nghttp3_frame_write_priority_update(uint8_t *p,
  76. const nghttp3_frame_priority_update *fr) {
  77. p = nghttp3_frame_write_hd(p, &fr->hd);
  78. p = nghttp3_put_varint(p, fr->pri_elem_id);
  79. if (fr->datalen) {
  80. p = nghttp3_cpymem(p, fr->data, fr->datalen);
  81. }
  82. return p;
  83. }
  84. size_t nghttp3_frame_write_priority_update_len(
  85. int64_t *ppayloadlen, const nghttp3_frame_priority_update *fr) {
  86. size_t payloadlen = nghttp3_put_varintlen(fr->pri_elem_id) + fr->datalen;
  87. *ppayloadlen = (int64_t)payloadlen;
  88. return nghttp3_put_varintlen(fr->hd.type) +
  89. nghttp3_put_varintlen((int64_t)payloadlen) + payloadlen;
  90. }
  91. int nghttp3_nva_copy(nghttp3_nv **pnva, const nghttp3_nv *nva, size_t nvlen,
  92. const nghttp3_mem *mem) {
  93. size_t i;
  94. uint8_t *data = NULL;
  95. size_t buflen = 0;
  96. nghttp3_nv *p;
  97. if (nvlen == 0) {
  98. *pnva = NULL;
  99. return 0;
  100. }
  101. for (i = 0; i < nvlen; ++i) {
  102. /* + 1 for null-termination */
  103. if ((nva[i].flags & NGHTTP3_NV_FLAG_NO_COPY_NAME) == 0) {
  104. buflen += nva[i].namelen + 1;
  105. }
  106. if ((nva[i].flags & NGHTTP3_NV_FLAG_NO_COPY_VALUE) == 0) {
  107. buflen += nva[i].valuelen + 1;
  108. }
  109. }
  110. buflen += sizeof(nghttp3_nv) * nvlen;
  111. *pnva = nghttp3_mem_malloc(mem, buflen);
  112. if (*pnva == NULL) {
  113. return NGHTTP3_ERR_NOMEM;
  114. }
  115. p = *pnva;
  116. data = (uint8_t *)(*pnva) + sizeof(nghttp3_nv) * nvlen;
  117. for (i = 0; i < nvlen; ++i) {
  118. p->flags = nva[i].flags;
  119. if (nva[i].flags & NGHTTP3_NV_FLAG_NO_COPY_NAME) {
  120. p->name = nva[i].name;
  121. p->namelen = nva[i].namelen;
  122. } else {
  123. if (nva[i].namelen) {
  124. memcpy(data, nva[i].name, nva[i].namelen);
  125. nghttp3_downcase(data, nva[i].namelen);
  126. }
  127. p->name = data;
  128. p->namelen = nva[i].namelen;
  129. data[p->namelen] = '\0';
  130. data += nva[i].namelen + 1;
  131. }
  132. if (nva[i].flags & NGHTTP3_NV_FLAG_NO_COPY_VALUE) {
  133. p->value = nva[i].value;
  134. p->valuelen = nva[i].valuelen;
  135. } else {
  136. if (nva[i].valuelen) {
  137. memcpy(data, nva[i].value, nva[i].valuelen);
  138. }
  139. p->value = data;
  140. p->valuelen = nva[i].valuelen;
  141. data[p->valuelen] = '\0';
  142. data += nva[i].valuelen + 1;
  143. }
  144. ++p;
  145. }
  146. return 0;
  147. }
  148. void nghttp3_nva_del(nghttp3_nv *nva, const nghttp3_mem *mem) {
  149. nghttp3_mem_free(mem, nva);
  150. }
  151. void nghttp3_frame_headers_free(nghttp3_frame_headers *fr,
  152. const nghttp3_mem *mem) {
  153. if (fr == NULL) {
  154. return;
  155. }
  156. nghttp3_nva_del(fr->nva, mem);
  157. }
  158. void nghttp3_frame_priority_update_free(nghttp3_frame_priority_update *fr,
  159. const nghttp3_mem *mem) {
  160. if (fr == NULL) {
  161. return;
  162. }
  163. nghttp3_mem_free(mem, fr->data);
  164. }