Ver código fonte

HPCC-8060 Add support for jsonp callbacks to ESP responses

Adding jsonp=function to an ESP json REST URL will wrap the json
response in the jsonp callback function.

Example:

http://myesp:8010/WsExample/method.json?jsonp=myfunction

would return:

myfunction({"name": "value"});

Signed-off-by: Anthony Fishbeck <Anthony.Fishbeck@lexisnexis.com>
Anthony Fishbeck 12 anos atrás
pai
commit
f798f60497

+ 5 - 0
esp/bindings/SOAP/Platform/soapbind.cpp

@@ -270,9 +270,14 @@ void CSoapComplexType::appendContent(IEspContext* ctx, MemoryBuffer& buffer, Str
     StringBuffer content;
     if (ctx && ctx->getResponseFormat()==ESPSerializationJSON)
     {
+        const char *jsonp = ctx->queryRequestParameters()->queryProp("jsonp");
+        if (jsonp && *jsonp)
+            content.append(jsonp).append('(');
         content.append('{');
         serializeStruct(ctx, content, (const char *)NULL);
         content.append('}');
+        if (jsonp && *jsonp)
+            content.append(");");
         mimetype.set("application/json; charset=UTF-8");
     }
     else

+ 9 - 3
esp/bindings/http/platform/httptransport.cpp

@@ -2340,10 +2340,12 @@ int CHttpResponse::sendException(IEspHttpException* e)
     return 0;
 }
 
-StringBuffer &toJSON(StringBuffer &json, IMultiException *me)
+StringBuffer &toJSON(StringBuffer &json, IMultiException *me, const char *callback)
 {
     IArrayOf<IException> &exs = me->getArray();
-    appendJSONName(json.set("{"), "Exceptions").append("{");
+    if (callback && *callback)
+        json.append(callback).append('(');
+    appendJSONName(json.append("{"), "Exceptions").append("{");
     appendJSONValue(json, "Source", me->source());
     appendJSONName(json, "Exception").append("[");
     ForEachItemIn(i, exs)
@@ -2357,6 +2359,8 @@ StringBuffer &toJSON(StringBuffer &json, IMultiException *me)
         json.append("}");
     }
     json.append("]}}");
+    if (callback && *callback)
+        json.append(");");
     return json;
 }
 
@@ -2372,9 +2376,11 @@ bool CHttpResponse::handleExceptions(IXslProcessor *xslp, IMultiException *me, c
         switch (context->getResponseFormat())
         {
         case ESPSerializationJSON:
+        {
             setContentType(HTTP_TYPE_APPLICATION_JSON_UTF8);
-            toJSON(content, me);
+            toJSON(content, me, context->queryRequestParameters()->queryProp("jsonp"));
             break;
+        }
         case ESPSerializationXML:
             setContentType(HTTP_TYPE_APPLICATION_XML);
             me->serialize(content);