[Groonga-commit] groonga/gcs [master] Handle XML SDF batch correctly (receive post data by myself because express doesn't have XML body parser)

Zurück zum Archiv-Index

YUKI Hiroshi null+****@clear*****
Mon Dec 10 18:42:19 JST 2012


YUKI Hiroshi	2012-12-10 18:42:19 +0900 (Mon, 10 Dec 2012)

  New Revision: 12ca47db37db45aff3396f60564691a880e27037
  https://github.com/groonga/gcs/commit/12ca47db37db45aff3396f60564691a880e27037

  Log:
    Handle XML SDF batch correctly (receive post data by myself because express doesn't have XML body parser)

  Modified files:
    lib/api/2011-02-01/batch.js

  Modified: lib/api/2011-02-01/batch.js (+39 -24)
===================================================================
--- lib/api/2011-02-01/batch.js    2012-12-10 18:26:52 +0900 (5685e96)
+++ lib/api/2011-02-01/batch.js    2012-12-10 18:42:19 +0900 (27b46d9)
@@ -22,6 +22,7 @@ function handleInvalidContentType(request, response) {
   var contentType = request.headers['content-type'];
   switch (contentType) {
     case 'application/json':
+    case 'text/xml':
     case 'application/xml':
       return false;
     default:
@@ -47,34 +48,48 @@ exports.createHandler = function(context, config) {
     if (handleInvalidContentLength(request, response)) return;
 
     var batches = request.body;
-    if (request.headers['content-type'] == 'application/xml')
-      batches = xml.toJSON(batches);
+    if (request.headers['content-type'] == 'text/xml' ||
+        request.headers['content-type'] == 'application/xml') {
+      // express has no parser for application/xml...
+      var body = '';
+      request.on('data', function (data) {
+        body += data;
+      });
+      request.on('end', function () {
+        batches = xml.toJSON(body);
+        processBatches();
+      });
+    } else {
+      processBatches();
+    }
 
-    var domain = new Domain({ source:        request,
-                              context:       context,
-                              documentsPath: config.documentsPath });
-    var processor = new BatchProcessor({
-          context: context,
-          domain: domain
-        });
+    function processBatches() {
+      var domain = new Domain({ source:        request,
+                                context:       context,
+                                documentsPath: config.documentsPath });
+      var processor = new BatchProcessor({
+            context: context,
+            domain: domain
+          });
 
-    try {
-      processor.validate(batches);
-    } catch (error) {
-      logger.error(error);
+      try {
+        processor.validate(batches);
+      } catch (error) {
+        logger.error(error);
 
-      return response.send(JSON.stringify(error.result));
-    }
+        return response.send(JSON.stringify(error.result));
+      }
 
-    processor.load(batches)
-      .next(function(result) {
-        response.contentType('application/json');
-        response.send(JSON.stringify(result));
-      })
-      .error(function(error) {
-        logger.error(error);
+      processor.load(batches)
+        .next(function(result) {
+          response.contentType('application/json');
+          response.send(JSON.stringify(result));
+        })
+        .error(function(error) {
+          logger.error(error);
 
-        response.send(error.message + '\n' + error.stack, 502);
-      });
+          response.send(error.message + '\n' + error.stack, 502);
+        });
+    }
   };
 };
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index