Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / lib / nghttp2_submit.c
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012, 2013 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 #include "nghttp2_submit.h"
26
27 #include <string.h>
28 #include <assert.h>
29
30 #include "nghttp2_session.h"
31 #include "nghttp2_frame.h"
32 #include "nghttp2_helper.h"
33 #include "nghttp2_priority_spec.h"
34
35 /* This function takes ownership of |nva_copy|. Regardless of the
36    return value, the caller must not free |nva_copy| after this
37    function returns. */
38 static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags,
39                                      int32_t stream_id,
40                                      const nghttp2_priority_spec *pri_spec,
41                                      nghttp2_nv *nva_copy, size_t nvlen,
42                                      const nghttp2_data_provider *data_prd,
43                                      void *stream_user_data,
44                                      uint8_t attach_stream) {
45   int rv;
46   uint8_t flags_copy;
47   nghttp2_outbound_item *item = NULL;
48   nghttp2_frame *frame = NULL;
49   nghttp2_headers_category hcat;
50   nghttp2_mem *mem;
51
52   mem = &session->mem;
53
54   if (stream_id == 0) {
55     rv = NGHTTP2_ERR_INVALID_ARGUMENT;
56     goto fail;
57   }
58
59   item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
60   if (item == NULL) {
61     rv = NGHTTP2_ERR_NOMEM;
62     goto fail;
63   }
64
65   nghttp2_outbound_item_init(item);
66
67   if (data_prd != NULL && data_prd->read_callback != NULL) {
68     item->aux_data.headers.data_prd = *data_prd;
69   }
70
71   item->aux_data.headers.stream_user_data = stream_user_data;
72   item->aux_data.headers.attach_stream = attach_stream;
73
74   flags_copy = (flags & (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY)) |
75                NGHTTP2_FLAG_END_HEADERS;
76
77   if (stream_id == -1) {
78     if (session->next_stream_id > INT32_MAX) {
79       rv = NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
80       goto fail;
81     }
82
83     stream_id = session->next_stream_id;
84     session->next_stream_id += 2;
85
86     hcat = NGHTTP2_HCAT_REQUEST;
87   } else {
88     /* More specific categorization will be done later. */
89     hcat = NGHTTP2_HCAT_HEADERS;
90   }
91
92   frame = &item->frame;
93
94   nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id, hcat,
95                              pri_spec, nva_copy, nvlen);
96
97   rv = nghttp2_session_add_item(session, item);
98
99   if (rv != 0) {
100     nghttp2_frame_headers_free(&frame->headers, mem);
101     goto fail2;
102   }
103
104   if (hcat == NGHTTP2_HCAT_REQUEST) {
105     return stream_id;
106   }
107
108   return 0;
109
110 fail:
111   /* nghttp2_frame_headers_init() takes ownership of nva_copy. */
112   nghttp2_nv_array_del(nva_copy, mem);
113 fail2:
114   nghttp2_mem_free(mem, item);
115
116   return rv;
117 }
118
119 static void adjust_priority_spec_weight(nghttp2_priority_spec *pri_spec) {
120   if (pri_spec->weight < NGHTTP2_MIN_WEIGHT) {
121     pri_spec->weight = NGHTTP2_MIN_WEIGHT;
122   } else if (pri_spec->weight > NGHTTP2_MAX_WEIGHT) {
123     pri_spec->weight = NGHTTP2_MAX_WEIGHT;
124   }
125 }
126
127 static int32_t submit_headers_shared_nva(nghttp2_session *session,
128                                          uint8_t flags, int32_t stream_id,
129                                          const nghttp2_priority_spec *pri_spec,
130                                          const nghttp2_nv *nva, size_t nvlen,
131                                          const nghttp2_data_provider *data_prd,
132                                          void *stream_user_data,
133                                          uint8_t attach_stream) {
134   int rv;
135   nghttp2_nv *nva_copy;
136   nghttp2_priority_spec copy_pri_spec;
137   nghttp2_mem *mem;
138
139   mem = &session->mem;
140
141   if (pri_spec) {
142     copy_pri_spec = *pri_spec;
143     adjust_priority_spec_weight(&copy_pri_spec);
144   } else {
145     nghttp2_priority_spec_default_init(&copy_pri_spec);
146   }
147
148   rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
149   if (rv < 0) {
150     return rv;
151   }
152
153   return submit_headers_shared(session, flags, stream_id, &copy_pri_spec,
154                                nva_copy, nvlen, data_prd, stream_user_data,
155                                attach_stream);
156 }
157
158 int32_t nghttp2_submit_trailer(nghttp2_session *session, int32_t stream_id,
159                                const nghttp2_nv *nva, size_t nvlen) {
160   return submit_headers_shared_nva(session, NGHTTP2_FLAG_END_STREAM, stream_id,
161                                    NULL, nva, nvlen, NULL, NULL, 0);
162 }
163
164 int32_t nghttp2_submit_headers(nghttp2_session *session, uint8_t flags,
165                                int32_t stream_id,
166                                const nghttp2_priority_spec *pri_spec,
167                                const nghttp2_nv *nva, size_t nvlen,
168                                void *stream_user_data) {
169   flags &= NGHTTP2_FLAG_END_STREAM;
170
171   if (pri_spec && !nghttp2_priority_spec_check_default(pri_spec)) {
172     flags |= NGHTTP2_FLAG_PRIORITY;
173   } else {
174     pri_spec = NULL;
175   }
176
177   return submit_headers_shared_nva(session, flags, stream_id, pri_spec, nva,
178                                    nvlen, NULL, stream_user_data, 0);
179 }
180
181 int nghttp2_submit_ping(nghttp2_session *session, uint8_t flags _U_,
182                         const uint8_t *opaque_data) {
183   return nghttp2_session_add_ping(session, NGHTTP2_FLAG_NONE, opaque_data);
184 }
185
186 int nghttp2_submit_priority(nghttp2_session *session, uint8_t flags _U_,
187                             int32_t stream_id,
188                             const nghttp2_priority_spec *pri_spec) {
189   int rv;
190   nghttp2_outbound_item *item;
191   nghttp2_frame *frame;
192   nghttp2_priority_spec copy_pri_spec;
193   nghttp2_mem *mem;
194
195   mem = &session->mem;
196
197   if (stream_id == 0 || pri_spec == NULL) {
198     return NGHTTP2_ERR_INVALID_ARGUMENT;
199   }
200
201   if (stream_id == pri_spec->stream_id) {
202     return NGHTTP2_ERR_INVALID_ARGUMENT;
203   }
204
205   copy_pri_spec = *pri_spec;
206
207   adjust_priority_spec_weight(&copy_pri_spec);
208
209   item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
210
211   if (item == NULL) {
212     return NGHTTP2_ERR_NOMEM;
213   }
214
215   nghttp2_outbound_item_init(item);
216
217   frame = &item->frame;
218
219   nghttp2_frame_priority_init(&frame->priority, stream_id, &copy_pri_spec);
220
221   rv = nghttp2_session_add_item(session, item);
222
223   if (rv != 0) {
224     nghttp2_frame_priority_free(&frame->priority);
225     nghttp2_mem_free(mem, item);
226
227     return rv;
228   }
229
230   return 0;
231 }
232
233 int nghttp2_submit_rst_stream(nghttp2_session *session, uint8_t flags _U_,
234                               int32_t stream_id, uint32_t error_code) {
235   if (stream_id == 0) {
236     return NGHTTP2_ERR_INVALID_ARGUMENT;
237   }
238
239   return nghttp2_session_add_rst_stream(session, stream_id, error_code);
240 }
241
242 int nghttp2_submit_goaway(nghttp2_session *session, uint8_t flags _U_,
243                           int32_t last_stream_id, uint32_t error_code,
244                           const uint8_t *opaque_data, size_t opaque_data_len) {
245   if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
246     return 0;
247   }
248   return nghttp2_session_add_goaway(session, last_stream_id, error_code,
249                                     opaque_data, opaque_data_len,
250                                     NGHTTP2_GOAWAY_AUX_NONE);
251 }
252
253 int nghttp2_submit_shutdown_notice(nghttp2_session *session) {
254   if (!session->server) {
255     return NGHTTP2_ERR_INVALID_STATE;
256   }
257   if (session->goaway_flags) {
258     return 0;
259   }
260   return nghttp2_session_add_goaway(session, (1u << 31) - 1, NGHTTP2_NO_ERROR,
261                                     NULL, 0,
262                                     NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE);
263 }
264
265 int nghttp2_submit_settings(nghttp2_session *session, uint8_t flags _U_,
266                             const nghttp2_settings_entry *iv, size_t niv) {
267   return nghttp2_session_add_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
268 }
269
270 int32_t nghttp2_submit_push_promise(nghttp2_session *session, uint8_t flags _U_,
271                                     int32_t stream_id, const nghttp2_nv *nva,
272                                     size_t nvlen,
273                                     void *promised_stream_user_data) {
274   nghttp2_outbound_item *item;
275   nghttp2_frame *frame;
276   nghttp2_nv *nva_copy;
277   uint8_t flags_copy;
278   int32_t promised_stream_id;
279   int rv;
280   nghttp2_mem *mem;
281
282   mem = &session->mem;
283
284   if (stream_id == 0 || nghttp2_session_is_my_stream_id(session, stream_id)) {
285     return NGHTTP2_ERR_INVALID_ARGUMENT;
286   }
287
288   if (!session->server) {
289     return NGHTTP2_ERR_PROTO;
290   }
291
292   /* All 32bit signed stream IDs are spent. */
293   if (session->next_stream_id > INT32_MAX) {
294     return NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
295   }
296
297   item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
298   if (item == NULL) {
299     return NGHTTP2_ERR_NOMEM;
300   }
301
302   nghttp2_outbound_item_init(item);
303
304   item->aux_data.headers.stream_user_data = promised_stream_user_data;
305
306   frame = &item->frame;
307
308   rv = nghttp2_nv_array_copy(&nva_copy, nva, nvlen, mem);
309   if (rv < 0) {
310     nghttp2_mem_free(mem, item);
311     return rv;
312   }
313
314   flags_copy = NGHTTP2_FLAG_END_HEADERS;
315
316   promised_stream_id = session->next_stream_id;
317   session->next_stream_id += 2;
318
319   nghttp2_frame_push_promise_init(&frame->push_promise, flags_copy, stream_id,
320                                   promised_stream_id, nva_copy, nvlen);
321
322   rv = nghttp2_session_add_item(session, item);
323
324   if (rv != 0) {
325     nghttp2_frame_push_promise_free(&frame->push_promise, mem);
326     nghttp2_mem_free(mem, item);
327
328     return rv;
329   }
330
331   return promised_stream_id;
332 }
333
334 int nghttp2_submit_window_update(nghttp2_session *session, uint8_t flags,
335                                  int32_t stream_id,
336                                  int32_t window_size_increment) {
337   int rv;
338   nghttp2_stream *stream = 0;
339   if (window_size_increment == 0) {
340     return 0;
341   }
342   flags = 0;
343   if (stream_id == 0) {
344     rv = nghttp2_adjust_local_window_size(
345         &session->local_window_size, &session->recv_window_size,
346         &session->recv_reduction, &window_size_increment);
347     if (rv != 0) {
348       return rv;
349     }
350   } else {
351     stream = nghttp2_session_get_stream(session, stream_id);
352     if (!stream) {
353       return 0;
354     }
355
356     rv = nghttp2_adjust_local_window_size(
357         &stream->local_window_size, &stream->recv_window_size,
358         &stream->recv_reduction, &window_size_increment);
359     if (rv != 0) {
360       return rv;
361     }
362   }
363
364   if (window_size_increment > 0) {
365     if (stream_id == 0) {
366       session->consumed_size =
367           nghttp2_max(0, session->consumed_size - window_size_increment);
368     } else {
369       stream->consumed_size =
370           nghttp2_max(0, stream->consumed_size - window_size_increment);
371     }
372
373     return nghttp2_session_add_window_update(session, flags, stream_id,
374                                              window_size_increment);
375   }
376   return 0;
377 }
378
379 static uint8_t set_request_flags(const nghttp2_priority_spec *pri_spec,
380                                  const nghttp2_data_provider *data_prd) {
381   uint8_t flags = NGHTTP2_FLAG_NONE;
382   if (data_prd == NULL || data_prd->read_callback == NULL) {
383     flags |= NGHTTP2_FLAG_END_STREAM;
384   }
385
386   if (pri_spec) {
387     flags |= NGHTTP2_FLAG_PRIORITY;
388   }
389
390   return flags;
391 }
392
393 int32_t nghttp2_submit_request(nghttp2_session *session,
394                                const nghttp2_priority_spec *pri_spec,
395                                const nghttp2_nv *nva, size_t nvlen,
396                                const nghttp2_data_provider *data_prd,
397                                void *stream_user_data) {
398   uint8_t flags;
399
400   if (pri_spec && nghttp2_priority_spec_check_default(pri_spec)) {
401     pri_spec = NULL;
402   }
403
404   flags = set_request_flags(pri_spec, data_prd);
405
406   return submit_headers_shared_nva(session, flags, -1, pri_spec, nva, nvlen,
407                                    data_prd, stream_user_data, 0);
408 }
409
410 static uint8_t set_response_flags(const nghttp2_data_provider *data_prd) {
411   uint8_t flags = NGHTTP2_FLAG_NONE;
412   if (data_prd == NULL || data_prd->read_callback == NULL) {
413     flags |= NGHTTP2_FLAG_END_STREAM;
414   }
415   return flags;
416 }
417
418 int nghttp2_submit_response(nghttp2_session *session, int32_t stream_id,
419                             const nghttp2_nv *nva, size_t nvlen,
420                             const nghttp2_data_provider *data_prd) {
421   uint8_t flags = set_response_flags(data_prd);
422   return submit_headers_shared_nva(session, flags, stream_id, NULL, nva, nvlen,
423                                    data_prd, NULL, 1);
424 }
425
426 int nghttp2_submit_data(nghttp2_session *session, uint8_t flags,
427                         int32_t stream_id,
428                         const nghttp2_data_provider *data_prd) {
429   int rv;
430   nghttp2_outbound_item *item;
431   nghttp2_frame *frame;
432   nghttp2_data_aux_data *aux_data;
433   uint8_t nflags = flags & NGHTTP2_FLAG_END_STREAM;
434   nghttp2_mem *mem;
435
436   mem = &session->mem;
437
438   if (stream_id == 0) {
439     return NGHTTP2_ERR_INVALID_ARGUMENT;
440   }
441
442   item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
443   if (item == NULL) {
444     return NGHTTP2_ERR_NOMEM;
445   }
446
447   nghttp2_outbound_item_init(item);
448
449   frame = &item->frame;
450   aux_data = &item->aux_data.data;
451   aux_data->data_prd = *data_prd;
452   aux_data->eof = 0;
453   aux_data->flags = nflags;
454
455   /* flags are sent on transmission */
456   nghttp2_frame_data_init(&frame->data, NGHTTP2_FLAG_NONE, stream_id);
457
458   rv = nghttp2_session_add_item(session, item);
459   if (rv != 0) {
460     nghttp2_frame_data_free(&frame->data);
461     nghttp2_mem_free(mem, item);
462     return rv;
463   }
464   return 0;
465 }
466
467 ssize_t nghttp2_pack_settings_payload(uint8_t *buf, size_t buflen,
468                                       const nghttp2_settings_entry *iv,
469                                       size_t niv) {
470   if (!nghttp2_iv_check(iv, niv)) {
471     return NGHTTP2_ERR_INVALID_ARGUMENT;
472   }
473
474   if (buflen < (niv * NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH)) {
475     return NGHTTP2_ERR_INSUFF_BUFSIZE;
476   }
477
478   return nghttp2_frame_pack_settings_payload(buf, iv, niv);
479 }