summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFulup Ar Foll <fulup@iot.bzh>2015-12-14 16:24:26 +0100
committerFulup Ar Foll <fulup@iot.bzh>2015-12-14 16:24:26 +0100
commit71f29a4ccfadb0eeed83479e61287252f9804fa7 (patch)
tree65e8eb927e1ae317bd2179fff7f850bc6e47d41f
parent2a66b8d8b0f36d0c6ec8ee6d9739d6e4945a1e70 (diff)
Integrated Post for Fileupload Test
-rw-r--r--include/local-def.h4
-rw-r--r--nbproject/configurations.xml38
-rw-r--r--src/afbs-api.c36
3 files changed, 71 insertions, 7 deletions
diff --git a/include/local-def.h b/include/local-def.h
index 2f8fd202..a5d48bfd 100644
--- a/include/local-def.h
+++ b/include/local-def.h
@@ -60,6 +60,7 @@
#define DEFLT_API_TIMEOUT 0 // default Plugin API Timeout
#define DEFLT_CACHE_TIMEOUT 100000 // default Static File Chache [Client Side Cache 100000~=1day]
#define DEFLT_AUTH_TOKEN NULL // expect for debug should == NULL
+#define DEFLT_HTTP_TIMEOUT 15 // Max MibMicroHttp timeout
typedef int BOOL;
#ifndef FALSE
@@ -199,7 +200,8 @@ typedef struct {
const char *url;
char *plugin;
char *api;
- char *post;
+ char *post; // post data in raw format
+ int len; // post data len
json_object *jresp;
AFB_clientCtx *client; // needed because libmicrohttp cannot create an empty response
int restfull; // request is resfull [uuid token provided]
diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml
index 129838ae..7a2bc668 100644
--- a/nbproject/configurations.xml
+++ b/nbproject/configurations.xml
@@ -61,12 +61,7 @@
<incDir>
<pElem>include</pElem>
<pElem>/usr/include/json-c</pElem>
- <pElem>/usr/include/libusb-1.0</pElem>
- <pElem>build/src</pElem>
</incDir>
- <preprocessorList>
- <Elem>WITH_RADIO_PLUGIN=1</Elem>
- </preprocessorList>
</cTool>
</makeTool>
<preBuild>
@@ -77,38 +72,69 @@
</makefileType>
<item path="src/afbs-api.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>build/src</pElem>
+ </incDir>
</cTool>
</item>
<item path="src/alsa-api.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>build/src</pElem>
+ </incDir>
</cTool>
</item>
<item path="src/config.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>build/src</pElem>
+ </incDir>
</cTool>
</item>
<item path="src/dbus-api.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>build/src</pElem>
+ </incDir>
</cTool>
</item>
<item path="src/http-svc.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>build/src</pElem>
+ </incDir>
</cTool>
</item>
<item path="src/main.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>build/src</pElem>
+ </incDir>
</cTool>
</item>
- <item path="src/radio-api.c" ex="false" tool="0" flavor2="0">
+ <item path="src/radio-api.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>/usr/include/libusb-1.0</pElem>
+ <pElem>build/src</pElem>
+ </incDir>
+ <preprocessorList>
+ <Elem>WITH_RADIO_PLUGIN=1</Elem>
+ </preprocessorList>
</cTool>
</item>
<item path="src/rest-api.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>build/src</pElem>
+ </incDir>
</cTool>
</item>
<item path="src/session.c" ex="false" tool="0" flavor2="2">
<cTool flags="0">
+ <incDir>
+ <pElem>build/src</pElem>
+ </incDir>
</cTool>
</item>
</conf>
diff --git a/src/afbs-api.c b/src/afbs-api.c
index 344377b2..c974efda 100644
--- a/src/afbs-api.c
+++ b/src/afbs-api.c
@@ -96,6 +96,8 @@ STATIC json_object* clientContextCheck (AFB_request *request) {
return (jresp);
}
+
+
// Close and Free context
STATIC json_object* clientContextReset (AFB_request *request) {
json_object *jresp;
@@ -112,6 +114,39 @@ STATIC json_object* clientContextReset (AFB_request *request) {
return (jresp);
}
+// Some file upload sample
+STATIC json_object* clientFileUpload (AFB_request *request) {
+ int fd;
+ json_object *jresp;
+ char filepath[512];
+ char *filename;
+
+ getQueryValue(request, "filename");
+ if (filename == NULL) return (jsonNewMessage(AFB_FAIL, "No Filename provided"));
+
+ // add an error code to respond
+ if (request->post == NULL) {
+ request->errcode=MHD_HTTP_UNAUTHORIZED;
+ return (jsonNewMessage(AFB_FAIL, "Post No Data"));
+ }
+
+ // This is simple test let's write file in config->session->filename
+ strncpy (filepath, request->config->configfile, sizeof(filepath));
+ strncat (filepath, "/", sizeof(filepath));
+ strncat (filepath, "/", sizeof(filepath));
+
+
+ if((fd = open(request->config->configfile, O_RDONLY)) < 0) {
+ return (jsonNewMessage(AFB_FAIL,"Fail to Upload file [%s] at [%s] error=\n", filename, filepath, strerror(errno)));
+ };
+
+ // write file on disk and free fd
+ write (fd, request->post, request->len);
+ close(fd);
+
+ return (jresp);
+}
+
// This function is call when Client Session Context is removed
// Note: when freeCtxCB==NULL standard free/malloc is called
STATIC void clientContextFree(AFB_clientCtx *client) {
@@ -125,6 +160,7 @@ STATIC AFB_restapi pluginApis[]= {
{"token-refresh" , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"},
{"token-check" , (AFB_apiCB)clientContextCheck ,"Check Client Context Token"},
{"token-reset" , (AFB_apiCB)clientContextReset ,"Close Client Context and Free resources"},
+ {"file-upload" , (AFB_apiCB)clientFileUpload ,"Demo for file upload"},
{NULL}
};