Beispiel #1
0
void test_nghttp2_hd_deflate_bound(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_nv nva[] = {MAKE_NV(":method", "GET"), MAKE_NV("alpha", "bravo")};
  nghttp2_bufs bufs;
  size_t bound, bound2;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nghttp2_hd_deflate_init(&deflater, mem);

  bound = nghttp2_hd_deflate_bound(&deflater, nva, ARRLEN(nva));

  CU_ASSERT(12 + 6 * 2 * 2 + nva[0].namelen + nva[0].valuelen + nva[1].namelen +
                nva[1].valuelen ==
            bound);

  nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva));

  CU_ASSERT(bound > (size_t)nghttp2_bufs_len(&bufs));

  bound2 = nghttp2_hd_deflate_bound(&deflater, nva, ARRLEN(nva));

  CU_ASSERT(bound == bound2);

  nghttp2_bufs_free(&bufs);
  nghttp2_hd_deflate_free(&deflater);
}
Beispiel #2
0
static void run_nghttp2_hd(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_bufs bufs;
  int rv;
  nghttp2_nv nva1[] = {
      MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"),
      MAKE_NV(":path", "/slashdot"),
      MAKE_NV("accept-encoding", "gzip, deflate"), MAKE_NV("foo", "bar")};
  nghttp2_nv nva2[] = {
      MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"),
      MAKE_NV(":path", "/style.css"), MAKE_NV("cookie", "nghttp2=FTW"),
      MAKE_NV("foo", "bar2")};

  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;
  }

  rv = deflate_inflate(&deflater, &inflater, &bufs, nva1, ARRLEN(nva1),
                       nghttp2_mem_fm());

  if (rv != 0) {
    goto deflate_hd_fail;
  }

  rv = deflate_inflate(&deflater, &inflater, &bufs, nva2, ARRLEN(nva2),
                       nghttp2_mem_fm());

  if (rv != 0) {
    goto deflate_hd_fail;
  }

deflate_hd_fail:
  nghttp2_hd_inflate_free(&inflater);
inflate_init_fail:
  nghttp2_hd_deflate_free(&deflater);
deflate_init_fail:
  nghttp2_bufs_free(&bufs);
}
Beispiel #3
0
void test_nghttp2_hd_deflate_same_indexed_repr(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_nv nva1[] = {MAKE_NV("cookie", "alpha"), MAKE_NV("cookie", "alpha")};
  nghttp2_nv nva2[] = {MAKE_NV("cookie", "alpha"), MAKE_NV("cookie", "alpha"),
                       MAKE_NV("cookie", "alpha")};
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nva_out out;
  int rv;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);
  CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem));
  CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem));

  /* Encode 2 same headers.  Emit 1 literal reprs and 1 index repr. */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1));
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(2 == out.nvlen);
  assert_nv_equal(nva1, out.nva, 2, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Encode 3 same headers.  This time, emits 3 index reprs. */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, ARRLEN(nva2));
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen == 3);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(3 == out.nvlen);
  assert_nv_equal(nva2, out.nva, 3, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Cleanup */
  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);
}
Beispiel #4
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);
}
Beispiel #5
0
void test_nghttp2_hd_no_index(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nghttp2_nv nva[] = {
      MAKE_NV(":method", "GET"), MAKE_NV(":method", "POST"),
      MAKE_NV(":path", "/foo"),  MAKE_NV("version", "HTTP/1.1"),
      MAKE_NV(":method", "GET"),
  };
  size_t i;
  nva_out out;
  int rv;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();

  /* 1st :method: GET can be indexable, last one is not */
  for (i = 1; i < ARRLEN(nva); ++i) {
    nva[i].flags = NGHTTP2_NV_FLAG_NO_INDEX;
  }

  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);

  nghttp2_hd_deflate_init(&deflater, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, ARRLEN(nva));
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(ARRLEN(nva) == out.nvlen);
  assert_nv_equal(nva, out.nva, ARRLEN(nva), mem);

  CU_ASSERT(out.nva[0].flags == NGHTTP2_NV_FLAG_NONE);
  for (i = 1; i < ARRLEN(nva); ++i) {
    CU_ASSERT(out.nva[i].flags == NGHTTP2_NV_FLAG_NO_INDEX);
  }

  nva_out_reset(&out, mem);

  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);
}
Beispiel #6
0
void test_nghttp2_nv_array_copy(void)
{
  nghttp2_nv *nva;
  ssize_t rv;
  nghttp2_nv emptynv[] = {MAKE_NV("", ""),
                          MAKE_NV("", "")};
  nghttp2_nv nv[] = {MAKE_NV("alpha", "bravo"),
                     MAKE_NV("charlie", "delta")};
  nghttp2_nv bignv;

  bignv.name = (uint8_t*)"echo";
  bignv.namelen = strlen("echo");
  bignv.valuelen = (1 << 14) - 1;
  bignv.value = malloc(bignv.valuelen);
  memset(bignv.value, '0', bignv.valuelen);

  rv = nghttp2_nv_array_copy(&nva, NULL, 0);
  CU_ASSERT(0 == rv);
  CU_ASSERT(NULL == nva);

  rv = nghttp2_nv_array_copy(&nva, emptynv, ARRLEN(emptynv));
  CU_ASSERT(0 == rv);
  CU_ASSERT(nva[0].namelen == 0);
  CU_ASSERT(nva[0].valuelen == 0);
  CU_ASSERT(nva[1].namelen == 0);
  CU_ASSERT(nva[1].valuelen == 0);

  nghttp2_nv_array_del(nva);

  rv = nghttp2_nv_array_copy(&nva, nv, ARRLEN(nv));
  CU_ASSERT(0 == rv);
  CU_ASSERT(nva[0].namelen == 5);
  CU_ASSERT(0 == memcmp("alpha", nva[0].name, 5));
  CU_ASSERT(nva[0].valuelen = 5);
  CU_ASSERT(0 == memcmp("bravo", nva[0].value, 5));
  CU_ASSERT(nva[1].namelen == 7);
  CU_ASSERT(0 == memcmp("charlie", nva[1].name, 7));
  CU_ASSERT(nva[1].valuelen == 5);
  CU_ASSERT(0 == memcmp("delta", nva[1].value, 5));

  nghttp2_nv_array_del(nva);

  /* Large header field is acceptable */
  rv = nghttp2_nv_array_copy(&nva, &bignv, 1);
  CU_ASSERT(0 == rv);

  nghttp2_nv_array_del(nva);

  free(bignv.value);
}
Beispiel #7
0
static int error_reply(nghttp2_session *session,
                       http2_stream_data *stream_data)
{
  int rv;
  int pipefd[2];
  nghttp2_nv hdrs[] = {
    MAKE_NV(":status", "404")
  };

  rv = pipe(pipefd);
  if(rv != 0) {
    warn("Could not create pipe");
    rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
                                   stream_data->stream_id,
                                   NGHTTP2_INTERNAL_ERROR);
    if(rv != 0) {
      warnx("Fatal error: %s", nghttp2_strerror(rv));
      return -1;
    }
    return 0;
  }
  write(pipefd[1], ERROR_HTML, sizeof(ERROR_HTML) - 1);
  close(pipefd[1]);
  stream_data->fd = pipefd[0];
  if(send_response(session, stream_data->stream_id, hdrs, ARRLEN(hdrs),
                   pipefd[0]) != 0) {
    close(pipefd[0]);
    return -1;
  }
  return 0;
}
Beispiel #8
0
static
int stream_have_eoh(h2stream *strm)
{
    session *sess = CONTAINER(strm->sess, session, S);
    request *req = CONTAINER(strm, request, R);
    nghttp2_nv hdr[] = {MAKE_NV(":status", "200"),
                        MAKE_NV("content-encoding", "text/plain")};
    req->wakeup = event_new(sess->serv->base, -1, EV_TIMEOUT|EV_PERSIST, wakeup_count, req);
    if(req->wakeup)
    {
        const struct timeval per = {2, 0};
        event_add(req->wakeup, &per);
    }
    printf("Send response\n");
    return h2session_respond(strm, hdr, ARRLEN(hdr));
}
Beispiel #9
0
void test_nghttp2_hd_inflate_newname_inc(void) {
  nghttp2_hd_inflater inflater;
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nghttp2_nv nv = MAKE_NV("x-rel", "nghttp2");
  nva_out out;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);
  nghttp2_hd_inflate_init(&inflater, mem);

  CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv, 1));

  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(blocklen > 0);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(1 == out.nvlen);
  assert_nv_equal(&nv, out.nva, 1, mem);
  CU_ASSERT(1 == inflater.ctx.hd_table.len);
  assert_nv_equal(
      &nv, &GET_TABLE_ENT(&inflater.ctx, NGHTTP2_STATIC_TABLE_LENGTH +
                                             inflater.ctx.hd_table.len - 1)->nv,
      1, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
}
Beispiel #10
0
/*
 * Submits the request |req| to the connection |connection|.  This
 * function does not send packets; just append the request to the
 * internal queue in |connection->session|.
 */
static void submit_request(struct Connection *connection, struct Request *req) {
  int32_t stream_id;
  /* Make sure that the last item is NULL */
  const nghttp2_nv nva[] = {MAKE_NV(":method", "GET"),
                            MAKE_NV_CS(":path", req->path),
                            MAKE_NV(":scheme", "https"),
                            MAKE_NV_CS(":authority", req->hostport),
                            MAKE_NV("accept", "*/*"),
                            MAKE_NV("user-agent", "nghttp2/" NGHTTP2_VERSION)};

  stream_id = nghttp2_submit_request(connection->session, NULL, nva,
                                     sizeof(nva) / sizeof(nva[0]), NULL, req);

  if (stream_id < 0) {
    diec("nghttp2_submit_request", stream_id);
  }

  req->stream_id = stream_id;
  printf("[INFO] Stream ID = %d\n", stream_id);
}
Beispiel #11
0
void test_nghttp2_hd_inflate_newname_noinc(void) {
  nghttp2_hd_inflater inflater;
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nghttp2_nv nv[] = {/* Expecting huffman for both */
                     MAKE_NV("my-long-content-length", "nghttp2"),
                     /* Expecting no huffman for both */
                     MAKE_NV("x", "y"),
                     /* Huffman for key only */
                     MAKE_NV("my-long-content-length", "y"),
                     /* Huffman for value only */
                     MAKE_NV("x", "nghttp2")};
  size_t i;
  nva_out out;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);
  nghttp2_hd_inflate_init(&inflater, mem);
  for (i = 0; i < ARRLEN(nv); ++i) {
    CU_ASSERT(0 == nghttp2_hd_emit_newname_block(&bufs, &nv[i],
                                                 NGHTTP2_HD_WITHOUT_INDEXING));

    blocklen = nghttp2_bufs_len(&bufs);

    CU_ASSERT(blocklen > 0);
    CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

    CU_ASSERT(1 == out.nvlen);
    assert_nv_equal(&nv[i], out.nva, 1, mem);
    CU_ASSERT(0 == inflater.ctx.hd_table.len);

    nva_out_reset(&out, mem);
    nghttp2_bufs_reset(&bufs);
  }

  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
}
Beispiel #12
0
void test_nghttp2_hd_public_api(void) {
  nghttp2_hd_deflater *deflater;
  nghttp2_hd_inflater *inflater;
  nghttp2_nv nva[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")};
  uint8_t buf[4096];
  size_t buflen;
  ssize_t blocklen;
  nghttp2_bufs bufs;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();

  CU_ASSERT(0 == nghttp2_hd_deflate_new(&deflater, 4096));
  CU_ASSERT(0 == nghttp2_hd_inflate_new(&inflater));

  buflen = nghttp2_hd_deflate_bound(deflater, nva, ARRLEN(nva));

  blocklen = nghttp2_hd_deflate_hd(deflater, buf, buflen, nva, ARRLEN(nva));

  CU_ASSERT(blocklen > 0);

  nghttp2_bufs_wrap_init(&bufs, buf, blocklen, mem);
  bufs.head->buf.last += blocklen;

  CU_ASSERT(blocklen == inflate_hd(inflater, NULL, &bufs, 0, mem));

  nghttp2_bufs_wrap_free(&bufs);

  nghttp2_hd_inflate_del(inflater);
  nghttp2_hd_deflate_del(deflater);

  /* See NGHTTP2_ERR_INSUFF_BUFSIZE */
  CU_ASSERT(0 == nghttp2_hd_deflate_new(&deflater, 4096));

  blocklen =
      nghttp2_hd_deflate_hd(deflater, buf, blocklen - 1, nva, ARRLEN(nva));

  CU_ASSERT(NGHTTP2_ERR_INSUFF_BUFSIZE == blocklen);

  nghttp2_hd_deflate_del(deflater);
}
Beispiel #13
0
/* Send HTTP request to the remote peer */
static void submit_request(http2_session_data *session_data) {
  int32_t stream_id;
  http2_stream_data *stream_data = session_data->stream_data;
  const char *uri = stream_data->uri;
  const struct http_parser_url *u = stream_data->u;
  nghttp2_nv hdrs[] = {
      MAKE_NV2(":method", "GET"),
      MAKE_NV(":scheme", &uri[u->field_data[UF_SCHEMA].off],
              u->field_data[UF_SCHEMA].len),
      MAKE_NV(":authority", stream_data->authority, stream_data->authoritylen),
      MAKE_NV(":path", stream_data->path, stream_data->pathlen)};
  fprintf(stderr, "Request headers:\n");
  print_headers(stderr, hdrs, ARRLEN(hdrs));
  stream_id = nghttp2_submit_request(session_data->session, NULL, hdrs,
                                     ARRLEN(hdrs), NULL, stream_data);
  if (stream_id < 0) {
    errx(1, "Could not submit HTTP request: %s", nghttp2_strerror(stream_id));
  }

  stream_data->stream_id = stream_id;
}
Beispiel #14
0
void test_nghttp2_hd_inflate_indname_noinc(void) {
  nghttp2_hd_inflater inflater;
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nghttp2_nv nv[] = {/* Huffman */
                     MAKE_NV("user-agent", "nghttp2"),
                     /* Expecting no huffman */
                     MAKE_NV("user-agent", "x")};
  size_t i;
  nva_out out;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);
  nghttp2_hd_inflate_init(&inflater, mem);

  for (i = 0; i < ARRLEN(nv); ++i) {
    CU_ASSERT(0 == nghttp2_hd_emit_indname_block(&bufs, 57, &nv[i], 0));

    blocklen = nghttp2_bufs_len(&bufs);

    CU_ASSERT(blocklen > 0);
    CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

    CU_ASSERT(1 == out.nvlen);
    assert_nv_equal(&nv[i], out.nva, 1, mem);
    CU_ASSERT(0 == inflater.ctx.hd_table.len);

    nva_out_reset(&out, mem);
    nghttp2_bufs_reset(&bufs);
  }

  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
}
Beispiel #15
0
static int on_request_recv_callback(nghttp2_session *session,
                                    int32_t stream_id, void *user_data)
{
  int fd;
  http2_session_data *session_data = (http2_session_data*)user_data;
  http2_stream_data *stream_data;
  nghttp2_nv hdrs[] = {
    MAKE_NV(":status", "200")
  };
  char *rel_path;

  stream_data = (http2_stream_data*)nghttp2_session_get_stream_user_data
    (session, stream_id);
  if(!stream_data->request_path) {
    if(error_reply(session, stream_data) != 0) {
      return NGHTTP2_ERR_CALLBACK_FAILURE;
    }
    return 0;
  }
  fprintf(stderr, "%s GET %s\n", session_data->client_addr,
          stream_data->request_path);
  if(!check_path(stream_data->request_path)) {
    if(error_reply(session, stream_data) != 0) {
      return NGHTTP2_ERR_CALLBACK_FAILURE;
    }
    return 0;
  }
  for(rel_path = stream_data->request_path; *rel_path == '/'; ++rel_path);
  fd = open(rel_path, O_RDONLY);
  if(fd == -1) {
    if(error_reply(session, stream_data) != 0) {
      return NGHTTP2_ERR_CALLBACK_FAILURE;
    }
    return 0;
  }
  stream_data->fd = fd;

  if(send_response(session, stream_id, hdrs, ARRLEN(hdrs), fd) != 0) {
    close(fd);
    return NGHTTP2_ERR_CALLBACK_FAILURE;
  }
  return 0;
}
Beispiel #16
0
void test_nghttp2_hd_inflate_indexed(void) {
  nghttp2_hd_inflater inflater;
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nghttp2_nv nv = MAKE_NV(":path", "/");
  nva_out out;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);
  nghttp2_hd_inflate_init(&inflater, mem);

  nghttp2_bufs_addb(&bufs, (1 << 7) | 4);

  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(1 == blocklen);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(1 == out.nvlen);

  assert_nv_equal(&nv, out.nva, 1, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* index = 0 is error */
  nghttp2_bufs_addb(&bufs, 1 << 7);

  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(1 == blocklen);
  CU_ASSERT(NGHTTP2_ERR_HEADER_COMP ==
            inflate_hd(&inflater, &out, &bufs, 0, mem));

  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
}
Beispiel #17
0
  }

static void deflate(nghttp2_hd_deflater *deflater,
                    nghttp2_hd_inflater *inflater, const nghttp2_nv *const nva,
                    size_t nvlen);

static int inflate_header_block(nghttp2_hd_inflater *inflater, uint8_t *in,
                                size_t inlen, int final);

int main(int argc _U_, char **argv _U_) {
  int rv;
  nghttp2_hd_deflater *deflater;
  nghttp2_hd_inflater *inflater;
  /* Define 1st header set.  This is looks like a HTTP request. */
  nghttp2_nv nva1[] = {
      MAKE_NV(":scheme", "https"), MAKE_NV(":authority", "example.org"),
      MAKE_NV(":path", "/"), MAKE_NV("user-agent", "libnghttp2"),
      MAKE_NV("accept-encoding", "gzip, deflate")};
  /* Define 2nd header set */
  nghttp2_nv nva2[] = {MAKE_NV(":scheme", "https"),
                       MAKE_NV(":authority", "example.org"),
                       MAKE_NV(":path", "/stylesheet/style.css"),
                       MAKE_NV("user-agent", "libnghttp2"),
                       MAKE_NV("accept-encoding", "gzip, deflate"),
                       MAKE_NV("referer", "https://example.org")};

  rv = nghttp2_hd_deflate_new(&deflater, 4096);

  if (rv != 0) {
    fprintf(stderr, "nghttp2_hd_deflate_init failed with error: %s\n",
            nghttp2_strerror(rv));
Beispiel #18
0
void test_nghttp2_hd_deflate_inflate(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_nv nv1[] = {
      MAKE_NV(":status", "200 OK"),
      MAKE_NV("access-control-allow-origin", "*"),
      MAKE_NV("cache-control", "private, max-age=0, must-revalidate"),
      MAKE_NV("content-length", "76073"),
      MAKE_NV("content-type", "text/html"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("expires", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("server", "Apache"),
      MAKE_NV("vary", "foobar"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "MISS from alphabravo"),
      MAKE_NV("x-cache-action", "MISS"),
      MAKE_NV("x-cache-age", "0"),
      MAKE_NV("x-cache-lookup", "MISS from alphabravo:3128"),
      MAKE_NV("x-lb-nocache", "true"),
  };
  nghttp2_nv nv2[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=56682045"),
      MAKE_NV("content-type", "text/css"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("expires", "Thu, 14 May 2015 07:22:57 GMT"),
      MAKE_NV("last-modified", "Tue, 14 May 2013 07:22:15 GMT"),
      MAKE_NV("vary", "Accept-Encoding"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128")};
  nghttp2_nv nv3[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=56682072"),
      MAKE_NV("content-type", "text/css"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("expires", "Thu, 14 May 2015 07:23:24 GMT"),
      MAKE_NV("last-modified", "Tue, 14 May 2013 07:22:13 GMT"),
      MAKE_NV("vary", "Accept-Encoding"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
  };
  nghttp2_nv nv4[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=56682022"),
      MAKE_NV("content-type", "text/css"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("expires", "Thu, 14 May 2015 07:22:34 GMT"),
      MAKE_NV("last-modified", "Tue, 14 May 2013 07:22:14 GMT"),
      MAKE_NV("vary", "Accept-Encoding"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
  };
  nghttp2_nv nv5[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=4461139"),
      MAKE_NV("content-type", "application/x-javascript"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("expires", "Mon, 16 Sep 2013 21:34:31 GMT"),
      MAKE_NV("last-modified", "Thu, 05 May 2011 09:15:59 GMT"),
      MAKE_NV("vary", "Accept-Encoding"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
  };
  nghttp2_nv nv6[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=18645951"),
      MAKE_NV("content-type", "application/x-javascript"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("expires", "Fri, 28 Feb 2014 01:48:03 GMT"),
      MAKE_NV("last-modified", "Tue, 12 Jul 2011 16:02:59 GMT"),
      MAKE_NV("vary", "Accept-Encoding"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
  };
  nghttp2_nv nv7[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=31536000"),
      MAKE_NV("content-type", "application/javascript"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("etag", "\"6807-4dc5b54e0dcc0\""),
      MAKE_NV("expires", "Wed, 21 May 2014 08:32:17 GMT"),
      MAKE_NV("last-modified", "Fri, 10 May 2013 11:18:51 GMT"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
  };
  nghttp2_nv nv8[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=31536000"),
      MAKE_NV("content-type", "application/javascript"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("etag", "\"41c6-4de7d28585b00\""),
      MAKE_NV("expires", "Thu, 12 Jun 2014 10:00:58 GMT"),
      MAKE_NV("last-modified", "Thu, 06 Jun 2013 14:30:36 GMT"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
  };
  nghttp2_nv nv9[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=31536000"),
      MAKE_NV("content-type", "application/javascript"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("etag", "\"19d6e-4dc5b35a541c0\""),
      MAKE_NV("expires", "Wed, 21 May 2014 08:32:18 GMT"),
      MAKE_NV("last-modified", "Fri, 10 May 2013 11:10:07 GMT"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
  };
  nghttp2_nv nv10[] = {
      MAKE_NV(":status", "304 Not Modified"),
      MAKE_NV("age", "0"),
      MAKE_NV("cache-control", "max-age=56682045"),
      MAKE_NV("content-type", "text/css"),
      MAKE_NV("date", "Sat, 27 Jul 2013 06:22:12 GMT"),
      MAKE_NV("expires", "Thu, 14 May 2015 07:22:57 GMT"),
      MAKE_NV("last-modified", "Tue, 14 May 2013 07:21:53 GMT"),
      MAKE_NV("vary", "Accept-Encoding"),
      MAKE_NV("via", "1.1 alphabravo (squid/3.x.x), 1.1 nghttpx"),
      MAKE_NV("x-cache", "HIT from alphabravo"),
      MAKE_NV("x-cache-lookup", "HIT from alphabravo:3128"),
  };
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();

  nghttp2_hd_deflate_init(&deflater, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  check_deflate_inflate(&deflater, &inflater, nv1, ARRLEN(nv1), mem);
  check_deflate_inflate(&deflater, &inflater, nv2, ARRLEN(nv2), mem);
  check_deflate_inflate(&deflater, &inflater, nv3, ARRLEN(nv3), mem);
  check_deflate_inflate(&deflater, &inflater, nv4, ARRLEN(nv4), mem);
  check_deflate_inflate(&deflater, &inflater, nv5, ARRLEN(nv5), mem);
  check_deflate_inflate(&deflater, &inflater, nv6, ARRLEN(nv6), mem);
  check_deflate_inflate(&deflater, &inflater, nv7, ARRLEN(nv7), mem);
  check_deflate_inflate(&deflater, &inflater, nv8, ARRLEN(nv8), mem);
  check_deflate_inflate(&deflater, &inflater, nv9, ARRLEN(nv9), mem);
  check_deflate_inflate(&deflater, &inflater, nv10, ARRLEN(nv10), mem);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);
}
Beispiel #19
0
void test_nghttp2_hd_change_table_size(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_nv nva[] = {MAKE_NV("alpha", "bravo"), MAKE_NV("charlie", "delta")};
  nghttp2_nv nva2[] = {MAKE_NV(":path", "/")};
  nghttp2_bufs bufs;
  ssize_t rv;
  nva_out out;
  ssize_t blocklen;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);

  nghttp2_hd_deflate_init(&deflater, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  /* inflater changes notifies 8000 max header table size */
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 8000));

  CU_ASSERT(4096 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);

  /* This will emit encoding context update with header table size 4096 */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(4096 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(4096 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* inflater changes header table size to 1024 */
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 1024));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 1024));

  CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(1024 == inflater.settings_hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(1024 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* inflater changes header table size to 0 */
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 0));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 0));

  CU_ASSERT(0 == deflater.ctx.hd_table.len);
  CU_ASSERT(0 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(0 == inflater.ctx.hd_table.len);
  CU_ASSERT(0 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(0 == inflater.settings_hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(0 == deflater.ctx.hd_table.len);
  CU_ASSERT(0 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(0 == inflater.ctx.hd_table.len);
  CU_ASSERT(0 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(0 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  /* Check table buffer is expanded */
  frame_pack_bufs_init(&bufs);

  nghttp2_hd_deflate_init2(&deflater, 8192, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  /* First inflater changes header table size to 8000 */
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 8000));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 8000));

  CU_ASSERT(8000 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(8000 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(8000 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(8000 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 16383));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 16383));

  CU_ASSERT(8192 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(16383 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(16383 == inflater.settings_hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(8192 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(8192 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(16383 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Lastly, check the error condition */

  rv = nghttp2_hd_emit_table_size(&bufs, 25600);
  CU_ASSERT(rv == 0);
  CU_ASSERT(NGHTTP2_ERR_HEADER_COMP ==
            inflate_hd(&inflater, &out, &bufs, 0, mem));

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  /* Check that encoder can handle the case where its allowable buffer
     size is less than default size, 4096 */
  nghttp2_hd_deflate_init2(&deflater, 1024, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);

  /* This emits context update with buffer size 1024 */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(2 == deflater.ctx.hd_table.len);
  CU_ASSERT(1024 == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(2 == inflater.ctx.hd_table.len);
  CU_ASSERT(1024 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(4096 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  /* Check that table size UINT32_MAX can be received */
  nghttp2_hd_deflate_init2(&deflater, UINT32_MAX, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, UINT32_MAX));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, UINT32_MAX));

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva, 2);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(UINT32_MAX == deflater.ctx.hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(UINT32_MAX == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(UINT32_MAX == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  /* Check that context update emitted twice */
  nghttp2_hd_deflate_init2(&deflater, 4096, mem);
  nghttp2_hd_inflate_init(&inflater, mem);

  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 0));
  CU_ASSERT(0 == nghttp2_hd_inflate_change_table_size(&inflater, 3000));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 0));
  CU_ASSERT(0 == nghttp2_hd_deflate_change_table_size(&deflater, 3000));

  CU_ASSERT(0 == deflater.min_hd_table_bufsize_max);
  CU_ASSERT(3000 == deflater.ctx.hd_table_bufsize_max);

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, 1);
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(3 < blocklen);
  CU_ASSERT(3000 == deflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(UINT32_MAX == deflater.min_hd_table_bufsize_max);

  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));
  CU_ASSERT(3000 == inflater.ctx.hd_table_bufsize_max);
  CU_ASSERT(3000 == inflater.settings_hd_table_bufsize_max);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);

  nghttp2_bufs_free(&bufs);
}
Beispiel #20
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);
}
Beispiel #21
0
void test_nghttp2_hd_deflate(void) {
  nghttp2_hd_deflater deflater;
  nghttp2_hd_inflater inflater;
  nghttp2_nv nva1[] = {MAKE_NV(":path", "/my-example/index.html"),
                       MAKE_NV(":scheme", "https"), MAKE_NV("hello", "world")};
  nghttp2_nv nva2[] = {MAKE_NV(":path", "/script.js"),
                       MAKE_NV(":scheme", "https")};
  nghttp2_nv nva3[] = {MAKE_NV("cookie", "k1=v1"), MAKE_NV("cookie", "k2=v2"),
                       MAKE_NV("via", "proxy")};
  nghttp2_nv nva4[] = {MAKE_NV(":path", "/style.css"),
                       MAKE_NV("cookie", "k1=v1"), MAKE_NV("cookie", "k1=v1")};
  nghttp2_nv nva5[] = {MAKE_NV(":path", "/style.css"),
                       MAKE_NV("x-nghttp2", "")};
  nghttp2_bufs bufs;
  ssize_t blocklen;
  nva_out out;
  int rv;
  nghttp2_mem *mem;

  mem = nghttp2_mem_default();
  frame_pack_bufs_init(&bufs);

  nva_out_init(&out);
  CU_ASSERT(0 == nghttp2_hd_deflate_init(&deflater, mem));
  CU_ASSERT(0 == nghttp2_hd_inflate_init(&inflater, mem));

  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva1, ARRLEN(nva1));
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(3 == out.nvlen);
  assert_nv_equal(nva1, out.nva, 3, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Second headers */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva2, ARRLEN(nva2));
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(2 == out.nvlen);
  assert_nv_equal(nva2, out.nva, 2, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Third headers, including same header field name, but value is not
     the same. */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva3, ARRLEN(nva3));
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(3 == out.nvlen);
  assert_nv_equal(nva3, out.nva, 3, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Fourth headers, including duplicate header fields. */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva4, ARRLEN(nva4));
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(3 == out.nvlen);
  assert_nv_equal(nva4, out.nva, 3, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Fifth headers includes empty value */
  rv = nghttp2_hd_deflate_hd_bufs(&deflater, &bufs, nva5, ARRLEN(nva5));
  blocklen = nghttp2_bufs_len(&bufs);

  CU_ASSERT(0 == rv);
  CU_ASSERT(blocklen > 0);
  CU_ASSERT(blocklen == inflate_hd(&inflater, &out, &bufs, 0, mem));

  CU_ASSERT(2 == out.nvlen);
  assert_nv_equal(nva5, out.nva, 2, mem);

  nva_out_reset(&out, mem);
  nghttp2_bufs_reset(&bufs);

  /* Cleanup */
  nghttp2_bufs_free(&bufs);
  nghttp2_hd_inflate_free(&inflater);
  nghttp2_hd_deflate_free(&deflater);
}
Beispiel #22
0
static void run_nghttp2_session_send(void) {
  nghttp2_session *session;
  nghttp2_session_callbacks callbacks;
  nghttp2_nv nv[] = {MAKE_NV(":host", "example.org"),
                     MAKE_NV(":scheme", "https")};
  nghttp2_data_provider data_prd;
  nghttp2_settings_entry iv[2];
  my_user_data ud;
  int rv;
  memset(&callbacks, 0, sizeof(nghttp2_session_callbacks));
  callbacks.send_callback = null_send_callback;

  data_prd.read_callback = fixed_length_data_source_read_callback;
  ud.data_source_length = 64 * 1024;

  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;

  rv = nghttp2_session_client_new3(&session, &callbacks, &ud, NULL,
                                   nghttp2_mem_fm());
  if (rv != 0) {
    goto client_new_fail;
  }
  rv = nghttp2_submit_request(session, NULL, nv, ARRLEN(nv), &data_prd, NULL);
  if (rv < 0) {
    goto fail;
  }
  rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, -1, NULL, nv,
                              ARRLEN(nv), NULL);
  if (rv < 0) {
    goto fail;
  }
  rv = nghttp2_session_send(session);
  if (rv != 0) {
    goto fail;
  }
  /* The HEADERS submitted by the previous nghttp2_submit_headers will
     have stream ID 3. Send HEADERS to that stream. */
  rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, 3, NULL, nv,
                              ARRLEN(nv), NULL);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM, 3, &data_prd);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_session_send(session);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, 3, NGHTTP2_CANCEL);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_session_send(session);
  if (rv != 0) {
    goto fail;
  }
  /* Sending against half-closed stream */
  rv = nghttp2_submit_headers(session, NGHTTP2_FLAG_NONE, 3, NULL, nv,
                              ARRLEN(nv), NULL);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM, 3, &data_prd);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_submit_ping(session, NGHTTP2_FLAG_NONE, NULL);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, 2);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_session_send(session);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_submit_goaway(session, NGHTTP2_FLAG_NONE, 100, NGHTTP2_NO_ERROR,
                             NULL, 0);
  if (rv != 0) {
    goto fail;
  }
  rv = nghttp2_session_send(session);
  if (rv != 0) {
    goto fail;
  }

fail:
  nghttp2_session_del(session);
client_new_fail:;
}