include string.h for strdup
[gssweb.git] / json_gssapi / src / GSSRequest.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  *
4  * For license details, see the LICENSE file in the root of this project.
5  *
6  */
7
8 #include <cstddef>
9 #include <stdexcept>
10 #include <string.h>
11
12 #include "commands/GSSAcquireCred.h"
13 #include "commands/GSSInitSecContext.h"
14 #include "commands/GSSImportName.h"
15 #include "commands/GSSDisplayName.h"
16 #include "GSSRequest.h"
17 #include "GSSException.h"
18
19 using std::bad_alloc;
20
21 GSSRequest::GSSRequest ( string jsonString )
22 {
23   /* Local variables */
24   /* Error checking */
25   /* Setup */
26   /* Main processing */
27   response = JSONObject();
28   cmd = NULL;
29   requestString = jsonString;
30   
31   /* Cleanup */
32   /* Return */
33 }
34
35 void GSSRequest::execute()
36 {
37   try {
38     /* variables */
39     /* Error checking */
40     /* Setup */
41     parseJSON();
42     getCommand();
43     
44     /* Main processing */
45     if (NULL != cmd)
46       cmd->execute();
47     
48     /* Cleanup */
49     /* Return */
50   }
51   catch (GSSException e)
52   {
53     delete(cmd);
54     cmd = NULL;
55     JSONObject return_values;
56     return_values.set("major_status", e.getMajor());
57     return_values.set("minor_status", e.getMinor());
58 //     response.set("error_message", e.what());
59   }
60 }
61
62
63
64 void GSSRequest::parseJSON()
65 {
66   /* variables */
67   json_error_t jsonErr;
68   
69   try {
70     JSONObject cookies;
71     request = JSONObject::load(requestString.c_str(), 0, &jsonErr);
72     cookies = request.get("cookies");
73     response.set("cookies", cookies );
74     response.set("method", request.get("method").string());
75   }
76   /* bad_alloc is thrown when JSONObject can't parse the input string as JSON */
77   catch ( bad_alloc& ) 
78   {
79     // Top-level response
80     response.set("error_message", "Could not parse the input JSON.");
81     response.set("original_message", requestString.c_str());
82   }
83 }
84
85
86 void GSSRequest::getCommand()
87 {
88   string method;
89   JSONObject arguments = request.get("arguments");
90   
91   /* Error checking */
92   /* Setup */
93   if (request.get("method").isNull() )
94     method = "";
95   else
96     method = string( request.get("method").string() );
97   
98   if ( "gss_import_name" == method ) 
99   {
100     cmd = new GSSImportName ( &arguments );
101   } 
102   else if ( "gss_init_sec_context" == method ) 
103   {
104     cmd = new GSSInitSecContext ( &arguments );
105   } 
106   else if ( "gss_acquire_cred" == method ) 
107   {
108     cmd = new GSSAcquireCred ( &arguments );
109   } 
110   else if ( "gss_display_name" == method )
111   {
112     cmd = new GSSDisplayName ( &arguments );
113   }
114   else 
115   {
116     string error_message = string("Unrecognized command: ") + method;
117     response.set("error_message", error_message.c_str() );
118     response.set("original_message", requestString.c_str());
119     cmd = NULL;
120   }
121 }
122
123 string GSSRequest::getResponse()
124 {
125   /* Variables */
126   JSONObject *return_values;
127   string gssResponse;
128   
129   /* Main processing */
130   // Put the return values into the response, assuming that the command
131   // was actually executed.
132   if (NULL != cmd)
133   {
134     return_values = cmd->toJSON();
135     response.set("return_values", *return_values);
136   }
137   
138   // Convert the response into a string to return.
139   gssResponse = string( response.dump() );
140   
141   /* Return */
142   return(gssResponse);
143 }
144
145
146
147 char *gss_request(char *json_string)
148 {
149   /* Variables */
150   GSSRequest *req = new GSSRequest(string(json_string));
151   
152   /* Error checking */
153   // An empty json_string could be an error, but GSSRequest does
154   // a good job of handling it.
155   
156   /* Setup */
157   /* Main processing */
158   req->execute();
159   return strdup(req->getResponse().c_str());
160 }
161
162 void deallocate_reply(char *reply)
163 {
164   delete(reply);
165 }