Change from fread() to read()
[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   std::streamsize threeTwoBits = 32 / sizeof(string::size_type);
30   
31   /* Error checking */
32   
33   /* Setup */
34   
35   /* Main processing */
36   do 
37   {
38     try 
39     {
40       // Read 32 bit length
41       len = 0;
42       readThisRound = readTotal = 0;
43       while(4 != readTotal)
44       {
45         readThisRound = read(0, ((&len) + readTotal), 4 - readTotal);
46         readTotal += readThisRound;
47       }
48       
49       // Reads the number of bytes indicated by the above read
50       input = new char[len + 1];
51       readTotal = readThisRound = 0;
52       while (readTotal < len)
53       {
54         readRemaining = len - readTotal;
55         readThisRound = read( 0, &(input[readTotal]), readRemaining);
56         if (-1 == readThisRound)
57           break;
58         else
59           readTotal += readThisRound;
60       }
61       // ... and null-terminate it
62       input[len] = '\0';
63       
64       
65       // Parse the JSON
66       JSONObject json = JSONObject::load(input, 0, &jsonErr);
67       delete[] input;
68       
69       // Oh, how I wish I could simply use: switch(json.get("method"))
70       c_str = json.get("method").string();
71       method = c_str;
72       if ("gss_import_name" == method)
73       {
74         cmd = new GSSImportName(&json);
75       }
76       else if ("gss_create_sec_context" == method)
77       {
78         cmd = new GSSCreateSecContextCommand(&json);
79       }
80       else if ("gss_acquire_cred" == method)
81       {
82         cmd = new GSSAcquireCred(&json);
83       }
84       else 
85       {
86         JSONObject response;
87         response.set("method", "unknown");
88         response.set("error_message", "Did not find a valid method to execute.");
89         output = response.dump();
90         len = output.length();
91         cout.write((char *)&len, threeTwoBits);
92         cout << output << endl;
93       
94         continue;
95       }
96
97       cmd->execute();
98       result = cmd->toJSON();
99       delete cmd;
100       
101       output = result->dump();
102       len = output.length();
103       
104       cout.write((char *)&len, threeTwoBits);
105       cout << output;
106       cout.flush();
107
108     }
109     catch ( std::bad_alloc& e )
110     {
111       JSONObject response;
112       JSONObject error;
113       response.set("method", "unknown");
114       response.set("error_message", "Could not parse the input JSON");
115       response.set("original message", input);
116       error.set("text", jsonErr.text);
117       error.set("source", jsonErr.source);
118       error.set("line", jsonErr.line);
119       error.set("column", jsonErr.column);
120       error.set("position", jsonErr.position);
121       response.set("error", error);
122       output = response.dump();
123       len = output.length();
124       cout.write((char *)&len, threeTwoBits);
125       cout << output << endl;
126     }
127   } while(1);
128   
129   return 0;
130 }