Http data server Fixed binary data retrieval

This commit is contained in:
Olof hagsand 2022-05-05 18:06:42 +02:00
parent 90474d1523
commit 7452813da7
7 changed files with 28 additions and 13 deletions

View file

@ -288,7 +288,7 @@ api_http_data_file(clicon_handle h,
char *suffix; char *suffix;
char *media; char *media;
int ret; int ret;
char *str = NULL; char *buf = NULL;
size_t sz; size_t sz;
clicon_debug(1, "%s", __FUNCTION__); clicon_debug(1, "%s", __FUNCTION__);
@ -347,11 +347,11 @@ api_http_data_file(clicon_handle h,
/* Unoptimized, no direct read but requires an extra copy, /* Unoptimized, no direct read but requires an extra copy,
* the cligen buf API should have some mechanism for this case without the extra copy. * the cligen buf API should have some mechanism for this case without the extra copy.
*/ */
if ((str = malloc(fsize + 1)) == NULL){ if ((buf = malloc(fsize)) == NULL){
clicon_err(OE_UNIX, errno, "malloc"); clicon_err(OE_UNIX, errno, "malloc");
goto done; goto done;
} }
if ((sz = fread(str, fsize, 1, f)) < 0){ if ((sz = fread(buf, fsize, 1, f)) < 0){
clicon_err(OE_UNIX, errno, "fread"); clicon_err(OE_UNIX, errno, "fread");
goto done; goto done;
} }
@ -361,8 +361,7 @@ api_http_data_file(clicon_handle h,
goto done; goto done;
goto ok; goto ok;
} }
str[fsize] = 0; if (cbuf_append_buf(cbdata, buf, fsize) < 0){
if (cbuf_append_str(cbdata, str) < 0){
clicon_err(OE_UNIX, errno, "cbuf_append_str"); clicon_err(OE_UNIX, errno, "cbuf_append_str");
goto done; goto done;
} }
@ -375,8 +374,8 @@ api_http_data_file(clicon_handle h,
ok: ok:
retval = 0; retval = 0;
done: done:
if (str) if (buf)
free(str); free(buf);
if (f) if (f)
fclose(f); fclose(f);
if (cbfile) if (cbfile)

View file

@ -145,7 +145,6 @@ restconf_reply_send(void *req0,
sd->sd_code = code; sd->sd_code = code;
if (cb != NULL){ if (cb != NULL){
if (cbuf_len(cb)){ if (cbuf_len(cb)){
cprintf(cb, "\r\n");
sd->sd_body_len = cbuf_len(cb); sd->sd_body_len = cbuf_len(cb);
if (head){ if (head){
cbuf_free(cb); cbuf_free(cb);

View file

@ -329,7 +329,10 @@ restconf_http1_reply(restconf_conn *rc,
cprintf(sd->sd_outp_buf, "\r\n"); cprintf(sd->sd_outp_buf, "\r\n");
/* Write a body */ /* Write a body */
if (sd->sd_body){ if (sd->sd_body){
cbuf_append_str(sd->sd_outp_buf, cbuf_get(sd->sd_body)); if (cbuf_append_buf(sd->sd_outp_buf, cbuf_get(sd->sd_body), cbuf_len(sd->sd_body)) < 0){
clicon_err(OE_RESTCONF, errno, "cbuf_append_buf");
goto done;
}
} }
retval = 0; retval = 0;
done: done:

BIN
test/clixon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -116,6 +116,9 @@ EOF
# remove read access # remove read access
chmod 660 $dir/www/data/noread.html chmod 660 $dir/www/data/noread.html
# bitmap
cp ./clixon.png $dir/www/data/
# Http test routine with arguments: # Http test routine with arguments:
# 1. proto:http/https # 1. proto:http/https
function testrun() function testrun()
@ -221,6 +224,15 @@ EOF
new "WWW options" new "WWW options"
expectpart "$(curl $CURLOPTS -X OPTIONS $proto://localhost/data/index.html)" 0 "HTTP/$HVER 200" "allow: OPTIONS,HEAD,GET" expectpart "$(curl $CURLOPTS -X OPTIONS $proto://localhost/data/index.html)" 0 "HTTP/$HVER 200" "allow: OPTIONS,HEAD,GET"
# Remove -i option for binary transfer
CURLOPTS2=$(echo $CURLOPTS | sed 's/i//')
new "WWW binary bitmap"
curl $CURLOPTS2 -X GET $proto://localhost/data/clixon.png -o $dir/foo.png
cmp $dir/foo.png $dir/www/data/clixon.png
if [ $? -ne 0 ]; then
err1 "$dir/foo.png $dir/www/data/example.css should be equal" "Not equal"
fi
# negative errors # negative errors
new "WWW get http not found" new "WWW get http not found"
expectpart "$(curl $CURLOPTS -X GET -H 'Accept: text/html' $proto://localhost/data/notfound.html)" 0 "HTTP/$HVER 404" "Content-Type: text/html" "<title>404 Not Found</title>" expectpart "$(curl $CURLOPTS -X GET -H 'Accept: text/html' $proto://localhost/data/notfound.html)" 0 "HTTP/$HVER 404" "Content-Type: text/html" "<title>404 Not Found</title>"

View file

@ -144,11 +144,12 @@ if [ $r -ne 0 ]; then
err1 "retval 0" $r err1 "retval 0" $r
fi fi
# XXX ftest har \n
# Only compare relevant data line # Only compare relevant data line
echo -n "<data>">> $ftest echo -n "<data>">> $ftest
cat $fdataxml >> $ftest cat $fdataxml >> $ftest
echo "</data> " >> $ftest echo -n "</data>" >> $ftest
# -i dont always work properly # -i (ignore case) dont always work properly
sed '/<data>/!d' $foutput > $foutput2 sed '/<data>/!d' $foutput > $foutput2
mv $foutput2 $foutput mv $foutput2 $foutput

View file

@ -163,7 +163,8 @@ fi
# Only compare relevant data line # Only compare relevant data line
echo -n "<data>">> $ftest echo -n "<data>">> $ftest
cat $fdataxml >> $ftest cat $fdataxml >> $ftest
echo "</data> " >> $ftest #echo "</data> " >> $ftest
echo -n "</data>" >> $ftest
sed '/<data>/!d' $foutput > $foutput2 sed '/<data>/!d' $foutput > $foutput2
mv $foutput2 $foutput mv $foutput2 $foutput