diff options
author | John Christopher Anderson <jchris@apache.org> | 2009-01-24 05:17:50 +0000 |
---|---|---|
committer | John Christopher Anderson <jchris@apache.org> | 2009-01-24 05:17:50 +0000 |
commit | 9b83ec394c830ce4f01fa693179c01826859b6e0 (patch) | |
tree | 3c1c63e10643d34b99d8595824acb9ad12b111f6 /share | |
parent | 7e6f05f3e5946feb7628f13360852e647f297df3 (diff) |
Improve show/list API and send external responses without chunked as it's not needed.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@737304 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'share')
-rw-r--r-- | share/server/main.js | 15 | ||||
-rw-r--r-- | share/www/script/couch_tests.js | 20 |
2 files changed, 24 insertions, 11 deletions
diff --git a/share/server/main.js b/share/server/main.js index d2b9b7e0..8e1f91cd 100644 --- a/share/server/main.js +++ b/share/server/main.js @@ -147,7 +147,7 @@ respondWith = function(req, responders) { var bestKey = keysByMime[bestMime]; var rFunc = responders[bestKey]; if (rFunc) { - var resp = rFunc(); + var resp = maybeWrapResponse(rFunc()); resp["headers"] = resp["headers"] || {}; resp["headers"]["Content-Type"] = bestMime; return resp; @@ -369,10 +369,19 @@ while (cmd = eval(readline())) { } } +function maybeWrapResponse(resp) { + var type = typeof resp; + if ((type == "string") || (type == "xml")) { + return {body:resp}; + } else { + return resp; + } +}; + function runRenderFunction(renderFun, args) { try { - var result = renderFun.apply(null, args); - respond(result); + var resp = renderFun.apply(null, args); + respond(maybeWrapResponse(resp)); } catch(e) { log("function raised error: "+e.toString()); log("stacktrace: "+e.stack); diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js index 9fea3be7..c87e01a5 100644 --- a/share/www/script/couch_tests.js +++ b/share/www/script/couch_tests.js @@ -2610,19 +2610,21 @@ var tests = { acceptSwitch: stringFun(function(head, row, req) { return respondWith(req, { html : function() { + // If you're outputting text and you're not setting + // any headers, you can just return a string. if (head) { - return {body : "HTML <ul>"}; + return "HTML <ul>"; } else if (row) { - return {body : '\n<li>Key: ' - +row.key+' Value: '+row.value+'</li>'}; + return '\n<li>Key: ' + +row.key+' Value: '+row.value+'</li>'; } else { // tail - return {body : "</ul>"}; + return "</ul>"; } }, xml : function() { if (head) { - return {body:'<feed xmlns="http://www.w3.org/2005/Atom">' - +'<title>Test XML Feed</title>'}; + return '<feed xmlns="http://www.w3.org/2005/Atom">' + +'<title>Test XML Feed</title>'; } else if (row) { // Becase Safari can't stand to see that dastardly // E4X outside of a string. Outside of tests you @@ -2631,9 +2633,11 @@ var tests = { entry.id = row.id; entry.title = row.key; entry.content = row.value; - return {body:entry}; + // We'll also let you return just an E4X object + // if you aren't setting headers. + return entry; } else { - return {body : "</feed>"}; + return "</feed>"; } } }) |