### Eclipse Workspace Patch 1.0
#P contributions-6
Index: modules/json_server/json_server.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/json_server/json_server.module,v
retrieving revision 1.8.4.2
diff -u -r1.8.4.2 json_server.module
--- modules/json_server/json_server.module	4 May 2010 20:00:36 -0000	1.8.4.2
+++ modules/json_server/json_server.module	28 Jun 2010 14:52:46 -0000
@@ -13,7 +13,7 @@
  */
 function json_server_server_info() {
   return array(
-    '#name' => 'JSON',
+    '#name' => 'JSON/P',
     '#path' => 'json'
   );
 }
@@ -31,42 +31,51 @@
  *  Implementation of hook_server().
  */
 function json_server_server() {
-if (!isset($_POST)) {
-  return drupal_to_js(array('#error' => TRUE, '#data' => "JSON server accepts POST requests only."));
-}
-  
   $methods = services_get_all();
   services_strip_hashes($methods);
-  $request = drupal_parse_json($_POST['method']);
+  $request_method = drupal_parse_json($_REQUEST['method']);
   $args = array();
-  
+
   foreach ($methods as $method) {
-    if ($method['method'] == $request) {
-      unset($_POST['q']);
-      unset($_POST['method']);
+    if ($method['method'] == $request_method) {
+      unset($_REQUEST['q']);
+      unset($_REQUEST['method']);
       $args = array();
       foreach($method['args'] as $arg) {
-        if(isset($_POST[$arg['name']])) {
-          $args[] = drupal_parse_json($_POST[$arg['name']]);
+        if(isset($_REQUEST[$arg['name']])) {
+          $args[] = drupal_parse_json($_REQUEST[$arg['name']]);
         }
         elseif($arg['optional'] == 0) {
-          return drupal_to_js(array("#error" => TRUE, "#data" => "Argument ". $arg['name'] ." not recieved"));
+          $response = array("#error" => TRUE, "#data" => "Argument ". $arg['name'] ." not recieved");
         }
         else {
           $args[$arg['name']] = NULL;
         }
       }
       $result = services_method_call($method['method'], $args);
-      if (is_array($result) && $result['error'] === TRUE)
-        return drupal_to_js(array('#error' => TRUE, '#data' => $result['message']));
-      
-      return drupal_to_js(array('#error' => FALSE, '#data' => $result));
+      if (is_array($result) && $result['#error'] === TRUE) {
+        $response = array('#error' => TRUE, '#data' => $result['#message']);
+      }
+
+      $response = array('#error' => FALSE, '#data' => $result);
     }
   }
 
-  return drupal_to_js(array('#error' => TRUE, '#data' => "Invalid method $request"));
+  $response = array('#error' => TRUE, '#data' => "Invalid method $request_method");
+
+  // Check if JSONP is active and callback is set
+  $json_server_jsonp = variable_get('json_server_jsonp', FALSE);
+  $callback = variable_get('json_server_jsonp_callback', 'callback');
+  if ($json_server_jsonp && isset($_REQUEST[$callback]) && ctype_alnum($_REQUEST[$callback])) {
+    // We are returning JavaScript, so tell the browser.
+    drupal_set_header('Content-Type: text/javascript; charset=utf-8');
+    return $_REQUEST[$callback] ."(". drupal_to_js($response) .");";
+  } else {
+    // We are returning JSON.
+    return drupal_json($response);
+  }
 }
-      
+
 
 /**
  *  Load the needed assets to run the server via js.
@@ -102,4 +111,36 @@
     }
   }
   return $v;
-}
\ No newline at end of file
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function json_server_form_services_admin_settings_alter(&$form, &$form_state) {
+  $form['authentication']['#weight'] = -20;
+  $form['json_server'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('JSON/P Server'),
+    '#weight' => -10,
+  );
+  $form['json_server']['json_server_jsonp'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Activate'),
+    '#description' => t('Hallo world, be cautious.'),
+    '#default_value' => variable_get('json_server_jsonp', FALSE),
+  );
+  $form['json_server']['json_server_jsonp_callback'] = array(
+    '#type' => 'textfield',
+    '#title' => t('JSONP callback'),
+    '#default_value' => variable_get('json_server_jsonp_callback', 'callback'),
+  );
+  $form['#submit'][] = 'json_server_form_services_admin_settings_alter_submit';
+}
+
+/**
+ * Settings form; Submit handler
+ */
+function json_server_form_services_admin_settings_alter_submit($form, &$form_state) {
+  variable_set('json_server_jsonp', $form['#post']['json_server_jsonp']);
+  variable_set('json_server_jsonp_callback', $form['#post']['json_server_jsonp_callback']);
+}
