Explicitly use 4 character length field for Debian.
[gssweb.git] / json_gssapi / main.cpp
1 #include <commands/GSSImportName.h>
2 #include <commands/GSSCreateSecContextCommand.h>
3 #include <commands/GSSAcquireCred.h>
4 #include <datamodel/GSSBuffer.h>
5 #include <exception>
6 #include <iostream>
7 #include <string>
8 #include <unistd.h>
9 #include <util_json.h>
10
11
12 using std::cin;
13 using std::cout;
14 using std::endl;
15 using std::getline;
16 using std::string;
17
18 int main(int argc, char **argv) {
19   /* Variables */
20   string method, output;
21   const char* c_str;
22   char *input;
23   JSONObject json;
24   JSONObject *result;
25   json_error_t jsonErr;
26   GSSCommand *cmd;
27   int len;
28   ssize_t readTotal, readThisRound, readRemaining;
29   
30   /* Error checking */
31   
32   /* Setup */
33   
34   /* Main processing */
35   do 
36   {
37     try 
38     {
39       // Read 32 bit length
40       len = 0;
41       readThisRound = readTotal = 0;
42       while(4 != readTotal)
43       {
44         readThisRound = read(0, ((&len) + readTotal), 4 - readTotal);
45         readTotal += readThisRound;
46       }
47       
48       // Reads the number of bytes indicated by the above read
49       input = new char[len + 1];
50       readTotal = readThisRound = 0;
51       while (readTotal < len)
52       {
53         readRemaining = len - readTotal;
54         readThisRound = read( 0, &(input[readTotal]), readRemaining);
55         if (-1 == readThisRound)
56           break;
57         else
58           readTotal += readThisRound;
59       }
60       // ... and null-terminate it
61       input[len] = '\0';
62       
63       
64       // Parse the JSON
65       JSONObject json = JSONObject::load(input, 0, &jsonErr);
66       delete[] input;
67       
68       if ( json.get("method").isNull() )
69       {
70         JSONObject response;
71         response.set("method", "unknown");
72         response.set("error_message", "Did not find a valid method to execute.");
73         output = response.dump();
74         len = output.length();
75         cout.write((char *)&len, 4);
76         cout << output << endl;
77         continue;
78       }
79
80       // Oh, how I wish I could simply use: switch(json.get("method"))
81       c_str = json.get("method").string();
82       method = c_str;
83       if ("gss_import_name" == method)
84       {
85         cmd = new GSSImportName(&json);
86       }
87       else if ("gss_create_sec_context" == method)
88       {
89         cmd = new GSSCreateSecContextCommand(&json);
90       }
91       else if ("gss_acquire_cred" == method)
92       {
93         cmd = new GSSAcquireCred(&json);
94       }
95       else 
96       {
97         JSONObject response;
98         response.set("method", "unknown");
99         response.set("error_message", "Did not find a valid method to execute.");
100         output = response.dump();
101         len = output.length();
102         cout.write((char *)&len, 4);
103         cout << output << endl;
104       
105         continue;
106       }
107
108       cmd->execute();
109       result = cmd->toJSON();
110       delete cmd;
111       
112       output = result->dump();
113       len = output.length();
114       
115       cout.write((char *)&len, 4);
116       cout << output;
117       cout.flush();
118
119     }
120     catch ( std::bad_alloc& e )
121     {
122       JSONObject response;
123       JSONObject error;
124       response.set("method", "unknown");
125       response.set("error_message", "Could not parse the input JSON");
126       response.set("original message", input);
127       error.set("text", jsonErr.text);
128       error.set("source", jsonErr.source);
129       error.set("line", jsonErr.line);
130       error.set("column", jsonErr.column);
131       error.set("position", jsonErr.position);
132       response.set("error", error);
133       output = response.dump();
134       len = output.length();
135       cout.write((char *)&len, 4);
136       cout << output << endl;
137     }
138   } while(1);
139   
140   return 0;
141 }