8

我目前正在尝试使用nghttp2来构建多部分消息。消息的结构应如下所示。

在此处输入图像描述

我应该使用nghttp2_submit_request(here)函数,nva作为我的 HTTP/2 标头,并nghttp2_data_provider *data_prd用于我的数据。但是,我仍然不明白如何准确地创建两条消息(带有两个消息头)。


更新:

我可以在源代码中描述我的想法吗?请看下文。在这里,我nghttp2_data_provider用来打开一个音频文件,并写入缓冲区。

ssize_t data_prd_read_callback(
    nghttp2_session *session, int32_t stream_id, uint8_t *buf, size_t length,
    uint32_t *data_flags, nghttp2_data_source *source, void *user_data) 
{

  printf("[INFO] C ----------------------------> S (DATA post body), length:%zu\n", length);

  int fd = source->fd;
  ssize_t r;
// writting my opened audio file into buffer
  while ((r = read(fd, buf, length)) == -1 && errno == EINTR); 
  printf("stream_id:%d, nread:%zu\r\n", stream_id, r);
  return nread;
}



void submit_postAudio(http2_session_data *session_data) {
  int32_t stream_id;
  http2_stream_data *stream_data = session_data->stream_data[STREAM_ID_REQUESTAUDIO];
  const char *uri = stream_data->uri;
  const struct http_parser_url *u = stream_data->u;
  char byLength = 0;

  const nghttp2_nv hdrs[] = {
  MAKE_NV(":method", "POST"),   MAKE_NV_CS(":path", stream_data->path),
  MAKE_NV(":scheme", "https"), MAKE_NV("accept", "*/*"),    
  MAKE_NV_CS("authorization", stream_data->access_token),
  MAKE_NV("content-type", "multipart/form-data; boundary=abcdefg123")
  };

  fprintf(stderr, "Request headers:\n");
  print_headers(stderr, hdrs, ARRLEN(hdrs));

  int fileDescriptor = open ("/my_audio.wmv", O_APPEND);  // open my audio file 
  nghttp2_data_provider data_prd;
  data_prd.source.fd = fileDescriptor   // set the file descriptor 
  data_prd.source.ptr = NULL;
  data_prd.read_callback = data_prd_read_callback;

  stream_id = nghttp2_submit_request(session_data->session, NULL, hdrs,
                                     ARRLEN(hdrs), &data_prd, stream_data);
  if (stream_id < 0) {
    errx(1, "Could not submit HTTP request: %s", nghttp2_strerror(stream_id));
  }

  stream_data->stream_id = stream_id;
}

让我感到困惑的是 1)如何在音频中添加标题(更具体的消息标题)。2)如何在我的 Json 文件之后附加它。

4

1 回答 1

1

多部分 mime 标头通常定义如下:

-----boundary_id
name: value  # header stuff here, ie. "name: value" per line. No spaces in name, all ASCII
CRLF         # End of header is defined with an extra linefeed, AKA a "null line", see ref 1.

content here, ends at next boundary id

-----boundary_id

参考 1

如果内容是二进制的,您通常会将其编码为具有固定行长度的 base-64(您也可以发送二进制数据,但必须注意额外的空格,例如最终换行)。可以包含说明这一点的标头字段名称:

Content-Transfer-Encoding: Base64

(对于二进制数据使用值“二进制”代替)。

边界 id 必须足够独特,以免成为任何其他内容的一部分。在主标题中定义时,它不使用双破折号,但您需要在它作为分隔符的任何地方添加这样的双破折号。

所以在这种情况下,整个消息看起来像这样:

name1: value1
name2: value2
name3: value3
x-comment: "as required by the specs (ignore this line)"
content-type: multipart/form-data; boundary: my_unique_boundary_id

--my_unique_boundary_id
Content-Disposition: form-data; name="metadata"
Content-Type: application/json; charset=UTF-08

{JSON data here}

--my_unique_boundary_id
Content-Disposition: form-data; name="audio"
Content-Type: application/octet-stream
Content-Transfer-Encoding: Base64
x-comment: "binary audio encoded as base-64 follows next"

UklGRuKlAABXQVZFZm10ICgAAAABAAEARKwAAIhYAQACABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAc2NvdLQBAAAAAAAAVGhlIFJoeXRobSBNYWNoaW5lICAgICAgICAgICAgICAgICAgICAgICAg
IFRSTTEAICA6MDAAAAAAAAAvADAwMDAwMDAwMDAwMAAAALkBTQABAAAAJQAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABKYXNvbiBE
b25uZWxseSAoYWthIGRqIHB1enpsZSkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgIDAwQwAAAAAAADEyMTMwMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
... etc.

--my_unique_boundary_id
于 2017-07-19T23:46:28.433 回答