1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-21 23:58:49 -05:00

http2: move the mem+len pair to the stream struct

This commit is contained in:
Daniel Stenberg 2015-04-29 17:00:53 +02:00
parent 84c6b6561f
commit 7bbac214f5
5 changed files with 44 additions and 23 deletions

View File

@ -171,6 +171,10 @@ CURLcode Curl_http_setup_conn(struct connectdata *conn)
http->error_code = NGHTTP2_NO_ERROR; http->error_code = NGHTTP2_NO_ERROR;
http->closed = FALSE; http->closed = FALSE;
/* where to store incoming data for this stream and how big the buffer is */
http->mem = conn->data->state.buffer;
http->len = BUFSIZE;
return CURLE_OK; return CURLE_OK;
} }

View File

@ -166,6 +166,9 @@ struct HTTP {
size_t datalen; /* the number of bytes left in data */ size_t datalen; /* the number of bytes left in data */
bool closed; /* TRUE on HTTP2 stream close */ bool closed; /* TRUE on HTTP2 stream close */
uint32_t error_code; /* HTTP/2 error code */ uint32_t error_code; /* HTTP/2 error code */
char *mem; /* points to a buffer in memory to store received data */
size_t len; /* size of the buffer 'mem' points to */
}; };
typedef int (*sending)(void); /* Curl_send */ typedef int (*sending)(void); /* Curl_send */
@ -177,8 +180,6 @@ struct http_conn {
nghttp2_session *h2; nghttp2_session *h2;
uint8_t binsettings[H2_BINSETTINGS_LEN]; uint8_t binsettings[H2_BINSETTINGS_LEN];
size_t binlen; /* length of the binsettings data */ size_t binlen; /* length of the binsettings data */
char *mem; /* points to a buffer in memory to store */
size_t len; /* size of the buffer 'mem' points to */
sending send_underlying; /* underlying send Curl_send callback */ sending send_underlying; /* underlying send Curl_send callback */
recving recv_underlying; /* underlying recv Curl_recv callback */ recving recv_underlying; /* underlying recv Curl_recv callback */
char *inbuf; /* buffer to receive data from underlying socket */ char *inbuf; /* buffer to receive data from underlying socket */

View File

@ -38,6 +38,8 @@
#include "curl_memory.h" #include "curl_memory.h"
#include "memdebug.h" #include "memdebug.h"
#define MIN(x,y) ((x)<(y)?(x):(y))
#if (NGHTTP2_VERSION_NUM < 0x000600) #if (NGHTTP2_VERSION_NUM < 0x000600)
#error too old nghttp2 version, upgrade! #error too old nghttp2 version, upgrade!
#endif #endif
@ -185,7 +187,6 @@ static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
void *userp) void *userp)
{ {
struct connectdata *conn = (struct connectdata *)userp; struct connectdata *conn = (struct connectdata *)userp;
struct http_conn *c = &conn->proto.httpc;
struct SessionHandle *data_s = NULL; struct SessionHandle *data_s = NULL;
struct HTTP *stream = NULL; struct HTTP *stream = NULL;
int rv; int rv;
@ -264,14 +265,14 @@ static int on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame,
Curl_add_buffer(stream->header_recvbuf, "\r\n", 2); Curl_add_buffer(stream->header_recvbuf, "\r\n", 2);
left = stream->header_recvbuf->size_used - stream->nread_header_recvbuf; left = stream->header_recvbuf->size_used - stream->nread_header_recvbuf;
ncopy = c->len < left ? c->len : left; ncopy = MIN(stream->len, left);
memcpy(c->mem, stream->header_recvbuf->buffer + memcpy(stream->mem, stream->header_recvbuf->buffer +
stream->nread_header_recvbuf, ncopy); stream->nread_header_recvbuf, ncopy);
stream->nread_header_recvbuf += ncopy; stream->nread_header_recvbuf += ncopy;
c->mem += ncopy; stream->mem += ncopy;
c->len -= ncopy; stream->len -= ncopy;
break; break;
case NGHTTP2_PUSH_PROMISE: case NGHTTP2_PUSH_PROMISE:
rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, rv = nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE,
@ -303,7 +304,6 @@ static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
const uint8_t *data, size_t len, void *userp) const uint8_t *data, size_t len, void *userp)
{ {
struct connectdata *conn = (struct connectdata *)userp; struct connectdata *conn = (struct connectdata *)userp;
struct http_conn *c = &conn->proto.httpc;
struct HTTP *stream; struct HTTP *stream;
struct SessionHandle *data_s; struct SessionHandle *data_s;
size_t nread; size_t nread;
@ -327,13 +327,15 @@ static int on_data_chunk_recv(nghttp2_session *session, uint8_t flags,
} }
stream = data_s->req.protop; stream = data_s->req.protop;
nread = c->len < len ? c->len : len; nread = MIN(stream->len, len);
memcpy(c->mem, data, nread); memcpy(stream->mem, data, nread);
c->mem += nread; stream->mem += nread;
c->len -= nread; stream->len -= nread;
DEBUGF(infof(conn->data, "%zu data written\n", nread)); DEBUGF(infof(conn->data, "%zu data received for stream %x "
"(%zu left in buffer)\n",
nread, stream_id, stream->len));
if(nread < len) { if(nread < len) {
stream->data = data + nread; stream->data = data + nread;
@ -570,7 +572,7 @@ static ssize_t data_source_read_callback(nghttp2_session *session,
(void)stream_id; (void)stream_id;
(void)source; (void)source;
nread = c->upload_len < length ? c->upload_len : length; nread = MIN(c->upload_len, length);
if(nread > 0) { if(nread > 0) {
memcpy(buf, c->upload_mem, nread); memcpy(buf, c->upload_mem, nread);
c->upload_mem += nread; c->upload_mem += nread;
@ -594,7 +596,7 @@ static nghttp2_settings_entry settings[] = {
{ NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE }, { NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, NGHTTP2_INITIAL_WINDOW_SIZE },
}; };
#define H2_BUFSIZE 4096 #define H2_BUFSIZE (1024)
static void freestreamentry(void *freethis) static void freestreamentry(void *freethis)
{ {
@ -761,15 +763,18 @@ static ssize_t http2_recv(struct connectdata *conn, int sockindex,
/* If there is body data pending for this stream to return, do that */ /* If there is body data pending for this stream to return, do that */
size_t left = size_t left =
stream->header_recvbuf->size_used - stream->nread_header_recvbuf; stream->header_recvbuf->size_used - stream->nread_header_recvbuf;
size_t ncopy = len < left ? len : left; size_t ncopy = MIN(len, left);
memcpy(mem, stream->header_recvbuf->buffer + stream->nread_header_recvbuf, memcpy(mem, stream->header_recvbuf->buffer + stream->nread_header_recvbuf,
ncopy); ncopy);
stream->nread_header_recvbuf += ncopy; stream->nread_header_recvbuf += ncopy;
infof(conn->data, "http2_recv: Got %d bytes from header_recvbuf\n",
(int)ncopy);
return ncopy; return ncopy;
} }
if(stream->data) { if(stream->data) {
nread = len < stream->datalen ? len : stream->datalen; nread = MIN(len, stream->datalen);
memcpy(mem, stream->data, nread); memcpy(mem, stream->data, nread);
stream->data += nread; stream->data += nread;
@ -780,14 +785,17 @@ static ssize_t http2_recv(struct connectdata *conn, int sockindex,
stream->data = NULL; stream->data = NULL;
stream->datalen = 0; stream->datalen = 0;
} }
infof(conn->data, "http2_recv: Got %d bytes from stream->data\n",
(int)nread);
return nread; return nread;
} }
conn->proto.httpc.mem = mem; /* remember where to store incoming data for this stream and how big the
conn->proto.httpc.len = len; buffer is */
stream->mem = mem;
stream->len = len;
infof(conn->data, "http2_recv: %d bytes buffer\n", infof(conn->data, "http2_recv: %d bytes buffer\n", stream->len);
conn->proto.httpc.len);
nread = ((Curl_recv*)httpc->recv_underlying)(conn, FIRSTSOCKET, nread = ((Curl_recv*)httpc->recv_underlying)(conn, FIRSTSOCKET,
httpc->inbuf, H2_BUFSIZE, httpc->inbuf, H2_BUFSIZE,
@ -828,8 +836,11 @@ static ssize_t http2_recv(struct connectdata *conn, int sockindex,
*err = CURLE_SEND_ERROR; *err = CURLE_SEND_ERROR;
return 0; return 0;
} }
if(len != httpc->len) { if(len != stream->len) {
return len - conn->proto.httpc.len; infof(conn->data, "http2_recv: returns %d for stream %x (%zu/%zu)\n",
len - stream->len, stream->stream_id,
len, stream->len);
return len - stream->len;
} }
/* If stream is closed, return 0 to signal the http routine to close /* If stream is closed, return 0 to signal the http routine to close
the connection */ the connection */
@ -844,9 +855,11 @@ static ssize_t http2_recv(struct connectdata *conn, int sockindex,
*err = CURLE_HTTP2; *err = CURLE_HTTP2;
return -1; return -1;
} }
DEBUGF(infof(conn->data, "http2_recv returns 0\n"));
return 0; return 0;
} }
*err = CURLE_AGAIN; *err = CURLE_AGAIN;
DEBUGF(infof(conn->data, "http2_recv returns -1, AGAIN\n"));
return -1; return -1;
} }

View File

@ -432,6 +432,7 @@ static CURLcode readwrite_data(struct SessionHandle *data,
else { else {
/* read nothing but since we wanted nothing we consider this an OK /* read nothing but since we wanted nothing we consider this an OK
situation to proceed from */ situation to proceed from */
DEBUGF(infof(data, "readwrite_data: we're done!\n"));
nread = 0; nread = 0;
} }

View File

@ -5945,6 +5945,8 @@ CURLcode Curl_done(struct connectdata **connp,
conn = *connp; conn = *connp;
data = conn->data; data = conn->data;
DEBUGF(infof(data, "Curl_done\n"));
if(conn->bits.done) if(conn->bits.done)
/* Stop if Curl_done() has already been called */ /* Stop if Curl_done() has already been called */
return CURLE_OK; return CURLE_OK;