Skip to content

Commit

Permalink
Add ad_http_get_content_length_stored(), Update ad_http_get_content()…
Browse files Browse the repository at this point in the history
… to null-terminate the return.
  • Loading branch information
wolkykim committed Jul 5, 2016
1 parent 692d5cb commit e6bcc19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/asyncd/ad_http_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ extern struct evbuffer *ad_http_get_outbuf(ad_conn_t *conn);

extern const char *ad_http_get_request_header(ad_conn_t *conn, const char *name);
extern off_t ad_http_get_content_length(ad_conn_t *conn);
extern size_t ad_http_get_content_length_stored(ad_conn_t *conn);
extern void *ad_http_get_content(ad_conn_t *conn, size_t maxsize, size_t *storedsize);
extern int ad_http_is_keepalive_request(ad_conn_t *conn);

Expand Down
19 changes: 17 additions & 2 deletions src/ad_http_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,24 @@ off_t ad_http_get_content_length(ad_conn_t *conn) {
return http->request.contentlength;
}


/**
* Return the actual size of data stored in in-buffer
*/
size_t ad_http_get_content_length_stored(ad_conn_t *conn) {
ad_http_t *http = (ad_http_t *) ad_conn_get_extra(conn);
return evbuffer_get_length(http->request.inbuf);
}

/**
* Remove content from the in-buffer.
*
* @param maxsize maximum length of data to pull up. 0 to pull up everything.
* The return data gets null terminated for convenience. For an example,
* if it reads 3 bytes, it will allocate 4 bytes and the 4th byte will
* be set to null terminator. `storedsized` will still return 3.
*
* @param maxsize maximum length of data to read. 0 to read everything.
* @param storedsize the size of data read and stored in the return.
*/
void *ad_http_get_content(ad_conn_t *conn, size_t maxsize, size_t *storedsize) {
ad_http_t *http = (ad_http_t *) ad_conn_get_extra(conn);
Expand All @@ -165,11 +179,12 @@ void *ad_http_get_content(ad_conn_t *conn, size_t maxsize, size_t *storedsize) {
if (readlen == 0)
return NULL;

void *data = malloc(readlen);
void *data = malloc(readlen + 1);
if (data == NULL)
return NULL;

size_t removedlen = evbuffer_remove(http->request.inbuf, data, readlen);
((char*)data)[removedlen] = '\0';
if (storedsize)
*storedsize = removedlen;

Expand Down

0 comments on commit e6bcc19

Please sign in to comment.