Esempio n. 1
0
static void run_nghttp2_frame_pack_headers(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_frame frame, oframe;
  nghttp2_bufs bufs;
  nghttp2_nv nv[] = {MAKE_NV(":host", "example.org"),
                     MAKE_NV(":scheme", "https")};
  int rv;
  nghttp2_nv *nva;
  size_t nvlen;

  rv = frame_pack_bufs_init(&bufs);

  if (rv != 0) {
    return;
  }

  rv = nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
  if (rv != 0) {
    goto deflate_init_fail;
  }
  rv = nghttp2_hd_inflate_init(&inflater, nghttp2_mem_fm());
  if (rv != 0) {
    goto inflate_init_fail;
  }
  nvlen = ARRLEN(nv);
  rv = nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm());
  if (rv < 0) {
    goto nv_copy_fail;
  }
  nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1,
                             NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
  rv = nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
  if (rv != 0) {
    goto fail;
  }
  rv = unpack_framebuf(&oframe, &bufs);
  if (rv != 0) {
    goto fail;
  }
  nghttp2_frame_headers_free(&oframe.headers, nghttp2_mem_fm());

fail:
  nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm());
nv_copy_fail:
  nghttp2_hd_inflate_free(&inflater);
inflate_init_fail:
  nghttp2_hd_deflate_free(&deflater);
deflate_init_fail:
  nghttp2_bufs_free(&bufs);
}
Esempio n. 2
0
void test_nghttp2_frame_pack_headers_frame_too_large(void)
{
  nghttp2_hd_deflater deflater;
  nghttp2_headers frame;
  nghttp2_bufs bufs;
  nghttp2_nv *nva;
  size_t big_vallen = NGHTTP2_HD_MAX_NV;
  nghttp2_nv big_hds[16];
  size_t big_hdslen = ARRLEN(big_hds);
  size_t i;
  int rv;

  frame_pack_bufs_init(&bufs);

  for(i = 0; i < big_hdslen; ++i) {
    big_hds[i].name = (uint8_t*)"header";
    big_hds[i].value = malloc(big_vallen+1);
    memset(big_hds[i].value, '0' + (int)i, big_vallen);
    big_hds[i].value[big_vallen] = '\0';
    big_hds[i].namelen = strlen((char*)big_hds[i].name);
    big_hds[i].valuelen = big_vallen;
    big_hds[i].flags = NGHTTP2_NV_FLAG_NONE;
  }

  nghttp2_nv_array_copy(&nva, big_hds, big_hdslen);
  nghttp2_hd_deflate_init(&deflater);
  nghttp2_frame_headers_init(&frame,
                             NGHTTP2_FLAG_END_STREAM|NGHTTP2_FLAG_END_HEADERS,
                             1000000007, NGHTTP2_HCAT_REQUEST,
                             NULL, nva, big_hdslen);
  rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater);
  CU_ASSERT(NGHTTP2_ERR_HEADER_COMP == rv);

  nghttp2_frame_headers_free(&frame);
  nghttp2_bufs_free(&bufs);
  for(i = 0; i < big_hdslen; ++i) {
    free(big_hds[i].value);
  }
  nghttp2_hd_deflate_free(&deflater);
}
Esempio n. 3
0
/* This function takes ownership of |nva_copy|. Regardless of the
   return value, the caller must not free |nva_copy| after this
   function returns. */
static int32_t submit_headers_shared(nghttp2_session *session, uint8_t flags,
                                     int32_t stream_id,
                                     const nghttp2_priority_spec *pri_spec,
                                     nghttp2_nv *nva_copy, size_t nvlen,
                                     const nghttp2_data_provider *data_prd,
                                     void *stream_user_data) {
  int rv;
  uint8_t flags_copy;
  nghttp2_outbound_item *item = NULL;
  nghttp2_frame *frame = NULL;
  nghttp2_headers_category hcat;
  nghttp2_mem *mem;

  mem = &session->mem;

  if (stream_id == 0) {
    rv = NGHTTP2_ERR_INVALID_ARGUMENT;
    goto fail;
  }

  if (stream_id == -1) {
    if ((int32_t)session->next_stream_id == pri_spec->stream_id) {
      rv = NGHTTP2_ERR_INVALID_ARGUMENT;
      goto fail;
    }
  } else if (stream_id == pri_spec->stream_id) {
    rv = NGHTTP2_ERR_INVALID_ARGUMENT;
    goto fail;
  }

  item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
  if (item == NULL) {
    rv = NGHTTP2_ERR_NOMEM;
    goto fail;
  }

  nghttp2_outbound_item_init(item);

  if (data_prd != NULL && data_prd->read_callback != NULL) {
    item->aux_data.headers.data_prd = *data_prd;
  }

  item->aux_data.headers.stream_user_data = stream_user_data;

  flags_copy =
      (uint8_t)((flags & (NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PRIORITY)) |
                NGHTTP2_FLAG_END_HEADERS);

  if (stream_id == -1) {
    if (session->next_stream_id > INT32_MAX) {
      rv = NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE;
      goto fail;
    }

    stream_id = (int32_t)session->next_stream_id;
    session->next_stream_id += 2;

    hcat = NGHTTP2_HCAT_REQUEST;
  } else {
    /* More specific categorization will be done later. */
    hcat = NGHTTP2_HCAT_HEADERS;
  }

  frame = &item->frame;

  nghttp2_frame_headers_init(&frame->headers, flags_copy, stream_id, hcat,
                             pri_spec, nva_copy, nvlen);

  rv = nghttp2_session_add_item(session, item);

  if (rv != 0) {
    nghttp2_frame_headers_free(&frame->headers, mem);
    goto fail2;
  }

  if (hcat == NGHTTP2_HCAT_REQUEST) {
    return stream_id;
  }

  return 0;

fail:
  /* nghttp2_frame_headers_init() takes ownership of nva_copy. */
  nghttp2_nv_array_del(nva_copy, mem);
fail2:
  nghttp2_mem_free(mem, item);

  return rv;
}
Esempio n. 4
0
void test_nghttp2_frame_pack_headers()
{
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_headers frame, oframe;
  nghttp2_bufs bufs;
  nghttp2_nv *nva;
  nghttp2_priority_spec pri_spec;
  ssize_t nvlen;
  nva_out out;
  ssize_t hdblocklen;
  int rv;

  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);
  nghttp2_hd_deflate_init(&deflater);
  nghttp2_hd_inflate_init(&inflater);

  nva = headers();
  nvlen = HEADERS_LENGTH;

  nghttp2_priority_spec_default_init(&pri_spec);

  nghttp2_frame_headers_init(&frame,
                             NGHTTP2_FLAG_END_STREAM |
                             NGHTTP2_FLAG_END_HEADERS,
                             1000000007, NGHTTP2_HCAT_REQUEST,
                             &pri_spec, nva, nvlen);
  rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater);

  nghttp2_bufs_rewind(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
  CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs));

  check_frame_header(nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN,
                     NGHTTP2_HEADERS,
                     NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS,
                     1000000007, &oframe.hd);
  /* We did not include PRIORITY flag */
  CU_ASSERT(NGHTTP2_DEFAULT_WEIGHT == oframe.pri_spec.weight);

  hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN;
  CU_ASSERT(hdblocklen ==
            inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN));

  CU_ASSERT(7 == out.nvlen);
  CU_ASSERT(nvnameeq("method", &out.nva[0]));
  CU_ASSERT(nvvalueeq("GET", &out.nva[0]));

  nghttp2_frame_headers_free(&oframe);
  nva_out_reset(&out);
  nghttp2_bufs_reset(&bufs);

  memset(&oframe, 0, sizeof(oframe));
  /* Next, include NGHTTP2_FLAG_PRIORITY */
  nghttp2_priority_spec_init(&frame.pri_spec, 1000000009, 12, 1);
  frame.hd.flags |= NGHTTP2_FLAG_PRIORITY;

  rv = nghttp2_frame_pack_headers(&bufs, &frame, &deflater);

  CU_ASSERT(0 == rv);
  CU_ASSERT(nghttp2_bufs_len(&bufs) > 0);
  CU_ASSERT(0 == unpack_framebuf((nghttp2_frame*)&oframe, &bufs));

  check_frame_header(nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN,
                     NGHTTP2_HEADERS,
                     NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS |
                     NGHTTP2_FLAG_PRIORITY,
                     1000000007, &oframe.hd);

  CU_ASSERT(1000000009 == oframe.pri_spec.stream_id);
  CU_ASSERT(12 == oframe.pri_spec.weight);
  CU_ASSERT(1 == oframe.pri_spec.exclusive);

  hdblocklen = nghttp2_bufs_len(&bufs) - NGHTTP2_FRAME_HDLEN
    - nghttp2_frame_priority_len(oframe.hd.flags);
  CU_ASSERT(hdblocklen ==
            inflate_hd(&inflater, &out, &bufs, NGHTTP2_FRAME_HDLEN
                       + nghttp2_frame_priority_len(oframe.hd.flags)));

  nghttp2_nv_array_sort(out.nva, out.nvlen);
  CU_ASSERT(nvnameeq("method", &out.nva[0]));

  nghttp2_frame_headers_free(&oframe);
  nva_out_reset(&out);
  nghttp2_bufs_reset(&bufs);

  nghttp2_bufs_free(&bufs);
  nghttp2_frame_headers_free(&frame);
  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);
}
Esempio n. 5
0
static void run_nghttp2_session_recv(void) {
  nghttp2_session *session;
  nghttp2_session_callbacks callbacks;
  nghttp2_hd_deflater deflater;
  nghttp2_frame frame;
  nghttp2_bufs bufs;
  nghttp2_nv nv[] = {MAKE_NV(":authority", "example.org"),
                     MAKE_NV(":scheme", "https")};
  nghttp2_settings_entry iv[2];
  my_user_data ud;
  data_feed df;
  int rv;
  nghttp2_nv *nva;
  size_t nvlen;

  rv = frame_pack_bufs_init(&bufs);

  if (rv != 0) {
    return;
  }

  memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
  callbacks.recv_callback = data_feed_recv_callback;
  ud.df = &df;

  nghttp2_failmalloc_pause();
  nvlen = ARRLEN(nv);
  nghttp2_nv_array_copy(&nva, nv, nvlen, nghttp2_mem_fm());
  nghttp2_hd_deflate_init(&deflater, nghttp2_mem_fm());
  nghttp2_session_server_new3(&session, &callbacks, &ud, NULL,
                              nghttp2_mem_fm());
  nghttp2_failmalloc_unpause();

  /* HEADERS */
  nghttp2_failmalloc_pause();
  nghttp2_frame_headers_init(&frame.headers, NGHTTP2_FLAG_END_STREAM, 1,
                             NGHTTP2_HCAT_REQUEST, NULL, nva, nvlen);
  nghttp2_frame_pack_headers(&bufs, &frame.headers, &deflater);
  nghttp2_frame_headers_free(&frame.headers, nghttp2_mem_fm());
  data_feed_init(&df, &bufs);
  nghttp2_bufs_reset(&bufs);

  nghttp2_failmalloc_unpause();

  rv = nghttp2_session_recv(session);
  if (rv != 0) {
    goto fail;
  }

  /* PING */
  nghttp2_failmalloc_pause();
  nghttp2_frame_ping_init(&frame.ping, NGHTTP2_FLAG_NONE, NULL);
  nghttp2_frame_pack_ping(&bufs, &frame.ping);
  nghttp2_frame_ping_free(&frame.ping);
  data_feed_init(&df, &bufs);
  nghttp2_bufs_reset(&bufs);

  nghttp2_failmalloc_unpause();

  rv = nghttp2_session_recv(session);
  if (rv != 0) {
    goto fail;
  }

  /* RST_STREAM */
  nghttp2_failmalloc_pause();
  nghttp2_frame_rst_stream_init(&frame.rst_stream, 1, NGHTTP2_PROTOCOL_ERROR);
  nghttp2_frame_pack_rst_stream(&bufs, &frame.rst_stream);
  nghttp2_frame_rst_stream_free(&frame.rst_stream);
  nghttp2_bufs_reset(&bufs);

  nghttp2_failmalloc_unpause();

  rv = nghttp2_session_recv(session);
  if (rv != 0) {
    goto fail;
  }

  /* SETTINGS */
  nghttp2_failmalloc_pause();
  iv[0].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
  iv[0].value = 4096;
  iv[1].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
  iv[1].value = 100;
  nghttp2_frame_settings_init(&frame.settings, NGHTTP2_FLAG_NONE,
                              nghttp2_frame_iv_copy(iv, 2, nghttp2_mem_fm()),
                              2);
  nghttp2_frame_pack_settings(&bufs, &frame.settings);
  nghttp2_frame_settings_free(&frame.settings, nghttp2_mem_fm());
  nghttp2_bufs_reset(&bufs);

  nghttp2_failmalloc_unpause();

  rv = nghttp2_session_recv(session);
  if (rv != 0) {
    goto fail;
  }

fail:
  nghttp2_bufs_free(&bufs);
  nghttp2_session_del(session);
  nghttp2_hd_deflate_free(&deflater);
}