From 7452813da7c77537e2ab4a9111b83913a8ab6ef7 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 5 May 2022 18:06:42 +0200 Subject: [PATCH] Http data server Fixed binary data retrieval --- apps/restconf/clixon_http_data.c | 13 ++++++------- apps/restconf/restconf_api_native.c | 1 - apps/restconf/restconf_http1.c | 5 ++++- test/clixon.png | Bin 0 -> 1343 bytes test/test_http_data.sh | 14 +++++++++++++- test/test_perf_restconf.sh | 5 +++-- test/test_perf_restconf_ssl.sh | 3 ++- 7 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 test/clixon.png diff --git a/apps/restconf/clixon_http_data.c b/apps/restconf/clixon_http_data.c index 2aef39b1..4a203e7f 100644 --- a/apps/restconf/clixon_http_data.c +++ b/apps/restconf/clixon_http_data.c @@ -288,7 +288,7 @@ api_http_data_file(clicon_handle h, char *suffix; char *media; int ret; - char *str = NULL; + char *buf = NULL; size_t sz; 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, * 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"); goto done; } - if ((sz = fread(str, fsize, 1, f)) < 0){ + if ((sz = fread(buf, fsize, 1, f)) < 0){ clicon_err(OE_UNIX, errno, "fread"); goto done; } @@ -361,8 +361,7 @@ api_http_data_file(clicon_handle h, goto done; goto ok; } - str[fsize] = 0; - if (cbuf_append_str(cbdata, str) < 0){ + if (cbuf_append_buf(cbdata, buf, fsize) < 0){ clicon_err(OE_UNIX, errno, "cbuf_append_str"); goto done; } @@ -375,8 +374,8 @@ api_http_data_file(clicon_handle h, ok: retval = 0; done: - if (str) - free(str); + if (buf) + free(buf); if (f) fclose(f); if (cbfile) diff --git a/apps/restconf/restconf_api_native.c b/apps/restconf/restconf_api_native.c index 4bbd800b..4d26fa7c 100644 --- a/apps/restconf/restconf_api_native.c +++ b/apps/restconf/restconf_api_native.c @@ -145,7 +145,6 @@ restconf_reply_send(void *req0, sd->sd_code = code; if (cb != NULL){ if (cbuf_len(cb)){ - cprintf(cb, "\r\n"); sd->sd_body_len = cbuf_len(cb); if (head){ cbuf_free(cb); diff --git a/apps/restconf/restconf_http1.c b/apps/restconf/restconf_http1.c index ae01df35..6a73307f 100644 --- a/apps/restconf/restconf_http1.c +++ b/apps/restconf/restconf_http1.c @@ -329,7 +329,10 @@ restconf_http1_reply(restconf_conn *rc, cprintf(sd->sd_outp_buf, "\r\n"); /* Write a 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; done: diff --git a/test/clixon.png b/test/clixon.png new file mode 100644 index 0000000000000000000000000000000000000000..a06ddb42ef13680ebfb225594772867acd6d21ab GIT binary patch literal 1343 zcmeAS@N?(olHy`uVBq!ia0y~yU@&4}U@+xiV_;xlw%WnTz`($k|H*Y zfq{Xuz$3Dlfq`2Hgc&d0t^32kz`$PO>Fdh=jFpv7lP$!@Y9#{$%Nb7>$B>MBZ)enI zObwMeZvTAF@w4{?Vx4CDJzur+=CVzew}S$^QkohaJ)1nk#%%Xzv$?0|hzNRGE0B{^6gz__Nmc{43adYH!N6uj0Ly(qvRJOHF~tp+NplEsNTj z4{x@g+OGQR>uV*Ksmf`L*EBBN77TSg_eRRT?z!y5?%9c7oOW#Z%6eptQ;FuazyyUH z2HtBNNq$PJn7%$QD|*hge1CgWW^;$f>1Bo;pDn}e`y~W;uPsy;Jib?-ZH-;y3-gop z&s0j?6CYh+5#G5)(52t;OX889!)|J~)CJp5RKM+*)w|;HN1=;_!k+OwUS&-^-G7x| z?&@>6vc0ABj;nf|(*FDz+#<&l6D4M;GO+ZiPT&x!Oi<=;@C+96da{Y#EJ|2DYFlyb z9kWBLv_3AJx-mrHnQQ8Yy`sKHjkvn4EDCiFF8q4iE#qb=?;fr{qSaT$8*C%V=F)^g?YhJ#L-)UuCqBP6?SO53MmpT7^=%^*N`((~p zw=)$k{)eoR9qhesXc**1T<&>c-C5#Qh?unnSuv9&{)K+rgee%~y8io?hzf5DbD!3QjOAPw>vvGaY z+Shkjcl^0oBf{8`v9!94L5D&1!4=Qz$38YF6{IoPUfPvs`Qqx`)j8ECK1 z{@jne8sL)qQGLt52xDdqX^Bn+2LIwE8_!rPUDsCbR?%nT=UMQ;lkqDTgQ;SF(UYoF zuCTdoK7QqIe$Q5(xN&Dws?5@x=6gAH6}eCNHSoxCYTf_p6)fXorL}8@$Rqdp7Ry2c zg&8%@b~T^a@Xoiago&S>H7;!J7m5D$vy)F*1vZ2nU&bA>ZMsX)@5(yv)YAfcgaAIMi%@U$yq5Ym~XuvqQH#?%l87a>6mMEk7WfoAu~gAH81;DOLe3 z8>}8J^>BBq>x<7W4cWnYKhTiLFj;Vx+5;=oPEq*hgTe~DWM4f DuKr-b literal 0 HcmV?d00001 diff --git a/test/test_http_data.sh b/test/test_http_data.sh index 7e766fda..699c3f1d 100755 --- a/test/test_http_data.sh +++ b/test/test_http_data.sh @@ -116,6 +116,9 @@ EOF # remove read access chmod 660 $dir/www/data/noread.html +# bitmap +cp ./clixon.png $dir/www/data/ + # Http test routine with arguments: # 1. proto:http/https function testrun() @@ -221,6 +224,15 @@ EOF new "WWW options" 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 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" "404 Not Found" @@ -254,7 +266,7 @@ EOF expectpart "$(curl $CURLOPTS -X POST -H 'Accept: text/html' -H "Content-Type: application/yang-data+json" -d '{"ietf-interfaces:interfaces":{"interface":{"name":"eth/0/0","type":"clixon-example:eth","enabled":true}}}' $proto://localhost/data/notfound.html)" 0 "HTTP/$HVER 405" "Content-Type: text/html" "405 Method Not Allowed" fi - + if [ $RC -ne 0 ]; then new "Kill restconf daemon" stop_restconf diff --git a/test/test_perf_restconf.sh b/test/test_perf_restconf.sh index 50f1fa41..2dc2a9b8 100755 --- a/test/test_perf_restconf.sh +++ b/test/test_perf_restconf.sh @@ -144,11 +144,12 @@ if [ $r -ne 0 ]; then err1 "retval 0" $r fi +# XXX ftest har \n # Only compare relevant data line echo -n "">> $ftest cat $fdataxml >> $ftest -echo " " >> $ftest -# -i dont always work properly +echo -n "" >> $ftest +# -i (ignore case) dont always work properly sed '//!d' $foutput > $foutput2 mv $foutput2 $foutput diff --git a/test/test_perf_restconf_ssl.sh b/test/test_perf_restconf_ssl.sh index 94ab0a21..fb49e0f4 100755 --- a/test/test_perf_restconf_ssl.sh +++ b/test/test_perf_restconf_ssl.sh @@ -163,7 +163,8 @@ fi # Only compare relevant data line echo -n "">> $ftest cat $fdataxml >> $ftest -echo " " >> $ftest +#echo " " >> $ftest +echo -n "" >> $ftest sed '//!d' $foutput > $foutput2 mv $foutput2 $foutput