Merge pull request #13 from justinjessada/master
[matches/MCTX3420.git] / testing / fastcgi-approach / fastcgi_test.c
1 #include "fcgi_stdio.h" /* fcgi library; put it first*/
2 #include <stdlib.h>
3
4 /*
5         But the suggestion was: FunctionName, variable_name (local or member),
6     Structure, ENUMVALUE, Extern_FunctionName, g_global
7 */
8
9 typedef struct Data Data;
10
11 typedef void (*ModuleHandler) (Data *data, const char *params);
12
13 static void SensorsHandler(Data *data, const char *params) {
14     printf("Sensors module!<br>");
15 }
16
17 /*
18    API Schema:
19    Sensors:
20    /cgi/sensors?get=x
21    *get=x is optional. Retrieves info for sensor with id x
22    Devices:
23    /cgi/devices?status=x&power=y&id=z
24    *status and power is optional
25    *status retrieves whether device with id x is operational
26    *power tells whether or not to power on/off the device with id z
27    
28    Response format:
29    200 OK if request was ok
30    400 bad request for malformed request
31       
32 */
33 int main (int argc, char *argv[])
34 {
35   Data *data = NULL;
36   int count = 0;
37
38   //FCGI Accept loop
39   while (FCGI_Accept() >= 0)   {
40     ModuleHandler module_handler = NULL;
41     const char *module = getenv("DOCUMENT_URI_LOCAL");
42     const char *params = getenv("QUERY_STRING");
43
44     if (!strcmp("sensors", module)) {
45         module_handler = SensorsHandler; //Replace with pointer to sensors handler
46     } else if (!strcmp("admin"), module) {
47         module_handler = NULL; //Replace with pointer to admin handler
48         printf("Admin module selected!\n");
49     }
50     
51     if (module_handler) {
52         printf("Content-type: text/html\r\n\r\n"); //Replace with actual type
53         module_handler(data, params);
54     } else {
55         printf("Status: 400 Bad Request\r\n"
56                "Content-type: text/html\r\n\r\n"
57                "<title>400 Bad Request</title>\n"
58                "Unknown module '%s' selected.<br>\n",
59                module);   
60     }
61     
62     //Debgging:
63     printf("Module: %s, Params: %s<br>\n", module, params);
64     printf("Request number %d, host <i>%s</i>\n",
65         count++, getenv("SERVER_HOSTNAME"));
66   }
67 }

UCC git Repository :: git.ucc.asn.au