Add win-build.bat for buildbot
[gssweb.git] / json_gssapi / main.cpp
index ec3cb53..26c23e1 100644 (file)
@@ -1,11 +1,21 @@
 #include <commands/GSSImportName.h>
-#include <commands/GSSCreateSecContextCommand.h>
+#include <commands/GSSInitSecContext.h>
 #include <commands/GSSAcquireCred.h>
 #include <datamodel/GSSBuffer.h>
 #include <exception>
 #include <iostream>
 #include <string>
+#ifdef WIN32
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <share.h>
+#else
+#include <unistd.h>
+#endif
 #include <util_json.h>
+#include <GSSRequest.h>
+
 
 using std::cin;
 using std::cout;
@@ -15,75 +25,83 @@ using std::string;
 
 int main(int argc, char **argv) {
   /* Variables */
-  string input, method;
-  const char* c_str;
-  JSONObject json;
-  JSONObject *result;
-  json_error_t jsonErr;
-  GSSCommand *cmd;
+  string output;
+  char *input;
+  size_t len;
   
   /* Error checking */
   
   /* Setup */
   
   /* Main processing */
+#ifdef WIN32
+  if (argc < 2) {
+      return -1;
+  }
+  int fd;
+  if (_sopen_s(&fd, argv[1], _O_BINARY, _SH_DENYNO, _S_IREAD) != 0)
+  {
+      cout << "error :" << errno << " opening file: " << argv[1] << "\n";
+      return -1;
+  }
+  struct _stat fs;
+  if (_fstat(fd, &fs) != 0) {
+      cout << "error: " << errno << " from _fstat.\n";
+      return -1;
+  }
+
+  FILE *f =_fdopen(fd, "rb");
+  if (f == NULL) {
+      cout << "error: " << errno << " from _fdopen.\n";
+      return -1;
+  }
+  len = fs.st_size;
+  input = new char[len+1];
+  size_t count = fread(input, 1, len, f) ;
+  if (count != len) {
+      cout << "expected " << len << " bytes from fread; got " << count << ".\n";
+      return -1;
+  }
+  fclose(f);
+#else
+  ssize_t readTotal, readThisRound, readRemaining;
   do 
   {
-    try 
+    // Read 32 bit length
+    len = 0;
+    readThisRound = readTotal = 0;
+    while(4 != readTotal)
     {
-      cout << "Give me some JSON: ";
-      getline(cin, input);
-      
-      c_str = input.c_str();
-      JSONObject json = JSONObject::load(c_str, 0, &jsonErr);
-      
-      // Oh, how I wish I could simply use: switch(json.get("method"))
-      c_str = json.get("method").string();
-      method = c_str;
-      if ("gss_import_name" == method)
-      {
-        cmd = new GSSImportName(&json);
-      }
-      else if ("gss_create_sec_context" == method)
-      {
-        cmd = new GSSCreateSecContextCommand(&json);
-      }
-      else if ("gss_acquire_cred" == method)
-      {
-        cmd = new GSSAcquireCred(&json);
-      }
-      else 
-      {
-        JSONObject response;
-        response.set("method", "unknown");
-        response.set("error_message", "Did not find a valid method to execute.");
-        cout << response.dump() << endl;
-      
-        continue;
-      }
-
-      cmd->execute();
-      result = cmd->toJSON();
-      delete cmd;
-      
-      cout << result->dump( JSON_INDENT(4) ) << endl;
-
+      readThisRound = read(0, ((&len) + readTotal), 4 - readTotal);
+      readTotal += readThisRound;
     }
-    catch ( std::bad_alloc )
+    
+    // Reads the number of bytes indicated by the above read
+    input = new char[len + 1];
+    readTotal = readThisRound = 0;
+    while (readTotal < len)
     {
-      JSONObject response;
-      JSONObject error;
-      response.set("method", "unknown");
-      response.set("error_message", "Could not parse the input JSON");
-      error.set("text", jsonErr.text);
-      error.set("source", jsonErr.source);
-      error.set("line", jsonErr.line);
-      error.set("column", jsonErr.column);
-      error.set("position", jsonErr.position);
-      response.set("error", error);
-      cout << response.dump() << endl;
+      readRemaining = len - readTotal;
+      readThisRound = read( 0, &(input[readTotal]), readRemaining);
+      if (-1 == readThisRound)
+        break;
+      else
+        readTotal += readThisRound;
     }
+    // ... and null-terminate it
+#endif
+    input[len] = '\0';
+    
+    GSSRequest *req = new GSSRequest(string(input));
+    req->execute();
+    output = req->getResponse();
+    len = output.length();
+    
+    cout.write((char *)&len, 4);
+    cout << output;
+    cout.flush();
+#ifndef WIN32
   } while(1);
-  
+#endif
   return 0;
 }