Integrated Post for Fileupload Test
authorFulup Ar Foll <fulup@iot.bzh>
Mon, 14 Dec 2015 15:24:26 +0000 (16:24 +0100)
committerFulup Ar Foll <fulup@iot.bzh>
Mon, 14 Dec 2015 15:24:26 +0000 (16:24 +0100)
include/local-def.h
nbproject/configurations.xml
src/afbs-api.c

index 2f8fd20..a5d48bf 100644 (file)
@@ -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]
index 129838a..7a2bc66 100644 (file)
             <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>
       </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>
index 344377b..c974efd 100644 (file)
@@ -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}
 };