* Fixed inefficient RESTCONF HTTP/1 read body by appending bufs instead of re-parsing

* Fixed: http1 by mistake included in fcgi build
* Test: restconf perf test uses restconf for initial setup instead of netconf
This commit is contained in:
Olof hagsand 2022-02-12 21:46:15 +01:00
parent 6ae749c9dd
commit 10886a31a2
6 changed files with 89 additions and 44 deletions

View file

@ -114,7 +114,10 @@ APPSRC += restconf_stream_$(with_restconf).c
endif
# internal http/1 parser
YACCOBJS =
ifeq ($(with_restconf),native)
YACCOBJS = lex.clixon_http1_parse.o clixon_http1_parse.tab.o
endif
APPOBJ = $(APPSRC:.c=.o) $(YACCOBJS)

View file

@ -464,29 +464,35 @@ http1_check_expect(clicon_handle h,
/*! Is there more data to be read?
*
* @param[in] h Clixon handle
* @param[in] sd Restconf stream data (for http1 only stream 0)
* @retval 1 OK, message is fully read, proceed to processing
* @retval 0 OK, but message is partially read, need more data before processing
* @retval -1 Error
* Use Content-Length header as an indicator on the status of reading an input message:
* 0: No Content-Length or 0
* Either message header not fully read OR header does not contain Content-Length
* 1: Content-Length found but body has fewer bytes, ie remaining bytes to read
* 2: Content-Length found and matches body length. No more bytes to read
* @param[in] h Clixon handle
* @param[in] sd Restconf stream data (for http1 only stream 0)
* @param[out] status 0-2, see description above
* @retval 0 OK, see status param
* @retval -1 Error
*/
int
http1_check_readmore(clicon_handle h,
restconf_stream_data *sd)
http1_check_content_length(clicon_handle h,
restconf_stream_data *sd,
int *status)
{
int retval = -1;
char *val;
int len;
if ((val = restconf_param_get(h, "HTTP_CONTENT_LENGTH")) != NULL &&
(len = atoi(val)) != 0){
if ((val = restconf_param_get(h, "HTTP_CONTENT_LENGTH")) == NULL ||
(len = atoi(val)) == 0)
*status = 0;
else{
if (cbuf_len(sd->sd_indata) < len)
goto readmore;
*status = 1;
else
*status = 2;
}
retval = 1;
done:
return retval;
readmore:
retval = 0;
goto done;
return retval;
}

View file

@ -44,6 +44,6 @@ int clixon_http1_parse_string(clicon_handle h, restconf_conn *rc, char *str);
int clixon_http1_parse_buf(clicon_handle h, restconf_conn *rc, char *buf, size_t n);
int restconf_http1_path_root(clicon_handle h, restconf_conn *rc);
int http1_check_expect(clicon_handle h, restconf_conn *rc, restconf_stream_data *sd);
int http1_check_readmore(clicon_handle h, restconf_stream_data *sd);
int http1_check_content_length(clicon_handle h, restconf_stream_data *sd, int *status);
#endif /* _RESTCONF_HTTP1_H_ */

View file

@ -499,6 +499,8 @@ restconf_connection(int s,
#ifdef HAVE_HTTP1
clicon_handle h;
restconf_stream_data *sd;
int status;
int http1_headers_read = 0; /* Dont re-parse headers, just append body */
#endif
clicon_debug(1, "%s %d", __FUNCTION__, s);
@ -578,15 +580,34 @@ restconf_connection(int s,
clicon_err(OE_RESTCONF, EINVAL, "restconf stream not found");
goto done;
}
/* multi-buffer for multiple reads */
totlen += n;
if ((totbuf = realloc(totbuf, totlen+1)) == NULL){
clicon_err(OE_UNIX, errno, "realloc");
goto done;
if (http1_headers_read){
if (cbuf_append_buf(sd->sd_indata, buf, n) < 0){
clicon_err(OE_UNIX, errno, "cbuf_append");
goto done;
}
}
memcpy(&totbuf[totlen-n], buf, n);
totbuf[totlen] = '\0';
if (clixon_http1_parse_string(h, rc, totbuf) < 0){
else {
/* multi-buffer for multiple reads */
totlen += n;
if ((totbuf = realloc(totbuf, totlen+1)) == NULL){
clicon_err(OE_UNIX, errno, "realloc");
goto done;
}
memcpy(&totbuf[totlen-n], buf, n);
totbuf[totlen] = '\0';
}
/*
* The following cases are handled after parsing
* - Parse error
* - More data to read?
* - clear and read more
* - No more data
* - send error
* - Parse OK
* -
*/
if (!http1_headers_read &&
clixon_http1_parse_string(h, rc, totbuf) < 0){
/* Maybe only for non-ssl ? */
if ((ret = clixon_event_poll(rc->rc_s)) < 0)
goto done;
@ -617,15 +638,31 @@ restconf_connection(int s,
}
}
/* Check whole message is read.
* ret == 0: need more bytes
* Only way this could happen is that body is read
* 0: No Content-Length or 0
* header does not contain Content-Length or is 0
* (OR: message header not fully read SHOULDNT HAPPEN IF BODY READ)
* 1: Content-Length found but body has fewer bytes, ie remaining bytes to read
* 2: Content-Length found and matches body length. No more bytes to read
*/
if ((ret = http1_check_readmore(h, sd)) < 0)
if ((ret = http1_check_content_length(h, sd, &status)) < 0)
goto done;
if (ret == 0){
if (native_clear_input(h, sd) < 0)
goto done;
switch (status){
case 1:
/* Next read: keep header state and only append inbody */
http1_headers_read++;
readmore++;
continue;
break;
break;
case 0:
case 2:
default:
break;
}
if (status == 0){
}
if (restconf_http1_path_root(h, rc) < 0)
goto done;