- Native restconf: use cligen buffers instead of libevent for replies

This commit is contained in:
Olof hagsand 2021-04-29 14:06:05 +02:00
parent 05009aed67
commit 0d7e644335
6 changed files with 250 additions and 117 deletions

View file

@ -63,6 +63,7 @@
#include "restconf_lib.h"
#include "restconf_api.h" /* Virtual api */
#include "restconf_native.h"
/*! Add HTTP header field name and value to reply, evhtp specific
* @param[in] req Evhtp http request handle
@ -81,8 +82,9 @@ restconf_reply_header(void *req0,
size_t vlen;
char *value = NULL;
va_list ap;
evhtp_header_t *evhdr;
evhtp_connection_t *conn;
restconf_conn_h *rc;
if (req == NULL || name == NULL || vfmt == NULL){
clicon_err(OE_CFG, EINVAL, "req, name or value is NULL");
return -1;
@ -103,11 +105,18 @@ restconf_reply_header(void *req0,
goto done;
}
va_end(ap);
if ((evhdr = evhtp_header_new(name, value, 0, 1)) == NULL){ /* 1: free after use */
clicon_err(OE_CFG, errno, "evhttp_header_new");
if ((conn = evhtp_request_get_connection(req)) == NULL){
clicon_err(OE_DAEMON, EFAULT, "evhtp_request_get_connection");
goto done;
}
if ((rc = conn->arg) == NULL){
clicon_err(OE_RESTCONF, EFAULT, "Internal error: restconf-conn-h is NULL: shouldnt happen");
goto done;
}
if (cvec_add_string(rc->rc_outp_hdrs, (char*)name, value) < 0){
clicon_err(OE_RESTCONF, errno, "cvec_add_string");
goto done;
}
evhtp_headers_add_header(req->headers_out, evhdr);
retval = 0;
done:
if (value)
@ -115,6 +124,60 @@ restconf_reply_header(void *req0,
return retval;
}
/*! Send reply
* @see htp__create_reply_
*/
#define rc_parser conn->parser /* XXX */
static int
native_send_reply(restconf_conn_h *rc,
evhtp_request_t *request,
evhtp_res code)
{
int retval = -1;
unsigned char major;
unsigned char minor;
cg_var *cv;
switch (request->proto) {
case EVHTP_PROTO_10:
if (request->flags & EVHTP_REQ_FLAG_KEEPALIVE) {
/* protocol is HTTP/1.0 and clients wants to keep established */
if (restconf_reply_header(request, "Connection", "keep-alive") < 0)
goto done;
}
major = htparser_get_major(request->rc_parser); /* XXX Look origin */
minor = htparser_get_minor(request->rc_parser);
break;
case EVHTP_PROTO_11:
if (!(request->flags & EVHTP_REQ_FLAG_KEEPALIVE)) {
/* protocol is HTTP/1.1 but client wanted to close */
if (restconf_reply_header(request, "Connection", "keep-alive") < 0)
goto done;
}
major = htparser_get_major(request->rc_parser);
minor = htparser_get_minor(request->rc_parser);
break;
default:
/* this sometimes happens when a response is made but paused before
* the method has been parsed */
major = 1;
minor = 0;
break;
}
cprintf(rc->rc_outp_buf, "HTTP/%u.%u %u %s\r\n", major, minor, code, restconf_code2reason(code));
/* Loop over headers */
cv = NULL;
while ((cv = cvec_each(rc->rc_outp_hdrs, cv)) != NULL)
cprintf(rc->rc_outp_buf, "%s: %s\r\n", cv_name_get(cv), cv_string_get(cv));
cprintf(rc->rc_outp_buf, "\r\n");
// cvec_reset(rc->rc_outp_hdrs); /* Is now done in restconf_connection but can be done here */
retval = 0;
done:
return retval;
}
/*! Send HTTP reply with potential message body
* @param[in] req Evhtp http request handle
* @param[in] cb Body as a cbuf, send if
@ -130,13 +193,13 @@ restconf_reply_send(void *req0,
int retval = -1;
const char *reason_phrase;
evhtp_connection_t *conn;
struct evbuffer *eb = NULL;
restconf_conn_h *rc;
clicon_debug(1, "%s code:%d", __FUNCTION__, code);
req->status = code;
if ((reason_phrase = restconf_code2reason(code)) == NULL)
reason_phrase="";
#if 0 /* XXX remove status header för evhtp? */
#if 0 /* XXX remove status header for evhtp? */
if (restconf_reply_header(req, "Status", "%d %s", code, reason_phrase) < 0)
goto done;
#endif
@ -144,33 +207,29 @@ restconf_reply_send(void *req0,
clicon_err(OE_DAEMON, EFAULT, "evhtp_request_get_connection");
goto done;
}
/* If body, add a content-length header */
if (cb != NULL && cbuf_len(cb)){
cprintf(cb, "\r\n");
if (restconf_reply_header(req, "Content-Length", "%d", cbuf_len(cb)) < 0)
goto done;
}
evhtp_send_reply(req, req->status);
else
if (restconf_reply_header(req, "Content-Length", "0") < 0)
goto done;
if ((rc = conn->arg) == NULL){
clicon_err(OE_RESTCONF, EFAULT, "Internal error: restconf-conn-h is NULL: shouldnt happen");
goto done;
}
/* Create reply and write headers */
if (native_send_reply(rc, req, code) < 0)
goto done;
req->flags |= EVHTP_REQ_FLAG_FINISHED; /* Signal to evhtp to read next request */
/* Write a body if cbuf is nonzero */
if (cb != NULL && cbuf_len(cb)){
/* Suboptimal, copy from cbuf to evbuffer */
if ((eb = evbuffer_new()) == NULL){
clicon_err(OE_RESTCONF, errno, "evbuffer_new");
goto done;
}
if (evbuffer_add(eb, cbuf_get(cb), cbuf_len(cb)) < 0){
clicon_err(OE_CFG, errno, "evbuffer_add");
goto done;
}
evhtp_send_reply_body(req, eb); /* conn->bev = eb, body is different */
cprintf(rc->rc_outp_buf, "%s", cbuf_get(cb));
}
evhtp_send_reply_end(req); /* just flag finished */
retval = 0;
done:
if (eb)
evhtp_safe_free(eb, evbuffer_free);
return retval;
}