void spdylay_outbound_item_free(spdylay_outbound_item *item)
{
  if(item == NULL) {
    return;
  }
  if(item->frame_cat == SPDYLAY_CTRL) {
    spdylay_frame_type frame_type;
    spdylay_frame *frame;
    frame_type = spdylay_outbound_item_get_ctrl_frame_type(item);
    frame = spdylay_outbound_item_get_ctrl_frame(item);
    switch(frame_type) {
    case SPDYLAY_SYN_STREAM:
      spdylay_frame_syn_stream_free(&frame->syn_stream);
      free(((spdylay_syn_stream_aux_data*)item->aux_data)->data_prd);
      break;
    case SPDYLAY_SYN_REPLY:
      spdylay_frame_syn_reply_free(&frame->syn_reply);
      break;
    case SPDYLAY_RST_STREAM:
      spdylay_frame_rst_stream_free(&frame->rst_stream);
      break;
    case SPDYLAY_SETTINGS:
      spdylay_frame_settings_free(&frame->settings);
      break;
    case SPDYLAY_NOOP:
      /* We don't have any public API to add NOOP, so here is
         unreachable. */
      assert(0);
    case SPDYLAY_PING:
      spdylay_frame_ping_free(&frame->ping);
      break;
    case SPDYLAY_GOAWAY:
      spdylay_frame_goaway_free(&frame->goaway);
      break;
    case SPDYLAY_HEADERS:
      spdylay_frame_headers_free(&frame->headers);
      break;
    case SPDYLAY_WINDOW_UPDATE:
      spdylay_frame_window_update_free(&frame->window_update);
      break;
    case SPDYLAY_CREDENTIAL:
      assert(0);
      break;
    }
  } else if(item->frame_cat == SPDYLAY_DATA) {
    spdylay_data *data_frame;
    data_frame = spdylay_outbound_item_get_data_frame(item);
    spdylay_frame_data_free(data_frame);
  } else {
    /* Unreachable */
    assert(0);
  }
  free(item->frame);
  free(item->aux_data);
}
static void run_spdylay_frame_pack_rst_stream(void)
{
  spdylay_frame frame;
  uint8_t *buf = NULL;
  size_t buflen = 0;
  spdylay_frame_rst_stream_init(&frame.rst_stream, SPDYLAY_PROTO_SPDY3, 1,
                                SPDYLAY_PROTOCOL_ERROR);
  spdylay_frame_pack_rst_stream(&buf, &buflen, &frame.rst_stream);
  free(buf);
  spdylay_frame_rst_stream_free(&frame.rst_stream);
}
void spdylay_outbound_item_free(spdylay_outbound_item *item)
{
  if(item == NULL) {
    return;
  }
  switch(item->frame_type) {
  case SPDYLAY_SYN_STREAM:
    spdylay_frame_syn_stream_free(&item->frame->syn_stream);
    free(((spdylay_syn_stream_aux_data*)item->aux_data)->data_prd);
    break;
  case SPDYLAY_SYN_REPLY:
    spdylay_frame_syn_reply_free(&item->frame->syn_reply);
    break;
  case SPDYLAY_RST_STREAM:
    spdylay_frame_rst_stream_free(&item->frame->rst_stream);
    break;
  case SPDYLAY_SETTINGS:
    spdylay_frame_settings_free(&item->frame->settings);
    break;
  case SPDYLAY_NOOP:
    /* We don't have any public API to add NOOP, so here is
       unreachable. */
    abort();
  case SPDYLAY_PING:
    spdylay_frame_ping_free(&item->frame->ping);
    break;
  case SPDYLAY_GOAWAY:
    spdylay_frame_goaway_free(&item->frame->goaway);
    break;
  case SPDYLAY_HEADERS:
    spdylay_frame_headers_free(&item->frame->headers);
    break;
  case SPDYLAY_WINDOW_UPDATE:
    spdylay_frame_window_update_free(&item->frame->window_update);
    break;
  case SPDYLAY_DATA:
    spdylay_frame_data_free(&item->frame->data);
    break;
  }
  free(item->frame);
  free(item->aux_data);
}
static void run_spdylay_session_recv(void)
{
  spdylay_session *session;
  spdylay_session_callbacks callbacks;
  spdylay_zlib deflater;
  spdylay_frame frame;
  uint8_t *buf = NULL, *nvbuf = NULL;
  size_t buflen = 0, nvbuflen = 0;
  ssize_t framelen;
  const char *nv[] = { ":host", "example.org",
                       ":scheme", "https",
                       NULL };
  spdylay_settings_entry iv[2];
  spdylay_mem_chunk proof;
  spdylay_mem_chunk *certs;
  size_t ncerts;
  my_user_data ud;
  data_feed df;
  int rv;

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

  spdylay_failmalloc_pause();
  spdylay_zlib_deflate_hd_init(&deflater, SPDYLAY_PROTO_SPDY3);
  spdylay_session_server_new(&session, SPDYLAY_PROTO_SPDY3, &callbacks, &ud);
  spdylay_failmalloc_unpause();

  /* SYN_STREAM */
  spdylay_failmalloc_pause();
  spdylay_frame_syn_stream_init(&frame.syn_stream, SPDYLAY_PROTO_SPDY3,
                                SPDYLAY_CTRL_FLAG_FIN, 1, 0, 2,
                                spdylay_frame_nv_copy(nv));
  framelen = spdylay_frame_pack_syn_stream(&buf, &buflen,
                                           &nvbuf, &nvbuflen,
                                           &frame.syn_stream, &deflater);
  spdylay_frame_syn_stream_free(&frame.syn_stream);
  data_feed_init(&df, buf, framelen);
  spdylay_failmalloc_unpause();

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

  /* PING */
  spdylay_failmalloc_pause();
  spdylay_frame_ping_init(&frame.ping, SPDYLAY_PROTO_SPDY3, 1);
  framelen = spdylay_frame_pack_ping(&buf, &buflen, &frame.ping);
  spdylay_frame_ping_free(&frame.ping);
  data_feed_init(&df, buf, framelen);
  spdylay_failmalloc_unpause();

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

  /* RST_STREAM */
  spdylay_failmalloc_pause();
  spdylay_frame_rst_stream_init(&frame.rst_stream, SPDYLAY_PROTO_SPDY3, 1,
                                SPDYLAY_PROTOCOL_ERROR);
  framelen = spdylay_frame_pack_rst_stream(&buf, &buflen, &frame.rst_stream);
  spdylay_frame_rst_stream_free(&frame.rst_stream);
  spdylay_failmalloc_unpause();

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

  /* SETTINGS */
  spdylay_failmalloc_pause();
  iv[0].settings_id = SPDYLAY_SETTINGS_UPLOAD_BANDWIDTH;
  iv[0].flags = SPDYLAY_ID_FLAG_SETTINGS_PERSIST_VALUE;
  iv[0].value = 256;
  iv[1].settings_id = SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS;
  iv[1].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
  iv[1].value = 100;
  spdylay_frame_settings_init(&frame.settings, SPDYLAY_PROTO_SPDY3,
                              SPDYLAY_FLAG_SETTINGS_CLEAR_SETTINGS,
                              spdylay_frame_iv_copy(iv, 2), 2);
  framelen = spdylay_frame_pack_settings(&buf, &buflen, &frame.settings);
  spdylay_frame_settings_free(&frame.settings);
  spdylay_failmalloc_unpause();

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

  /* CREDENTIAL */
  spdylay_failmalloc_pause();
  proof.data = (uint8_t*)strcopy("PROOF");
  proof.length = strlen("PROOF");
  ncerts = 2;
  certs = malloc(sizeof(spdylay_mem_chunk)*ncerts);
  certs[0].data = (uint8_t*)strcopy("CERT0");
  certs[0].length = strlen("CERT0");
  certs[1].data = (uint8_t*)strcopy("CERT1");
  certs[1].length = strlen("CERT1");
  spdylay_frame_credential_init(&frame.credential, SPDYLAY_PROTO_SPDY3,
                                1, &proof, certs, ncerts);
  framelen = spdylay_frame_pack_credential(&buf, &buflen, &frame.credential);
  spdylay_frame_credential_free(&frame.credential);
  spdylay_failmalloc_unpause();

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

 fail:
  free(buf);
  free(nvbuf);
  spdylay_session_del(session);
  spdylay_zlib_deflate_free(&deflater);
}