This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
jsonrpc_server.module
74 lines (67 loc) · 2.26 KB
/
jsonrpc_server.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
// $Id$
function jsonrpc_server_server_info() {
return array(
'#name' => 'JSON-RPC',
'#path' => 'json-rpc',
);
}
function jsonrpc_server_server() {
require_once('JsonRpcServer.php');
$GLOBALS['devel_shutdown'] = false;
// TODO: Add settings page with options for disabling features that doesn't
// follow the JSON-RPC spec. One thing that definitely could be disabled is
// the GET request handling, regardless of conformance to spec.
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'POST':
// Omit charset parameter
list($content_type) = explode(';', $_SERVER['CONTENT_TYPE'], 2);
switch($content_type) {
// Handling of a standard JSON-RPC call
case 'application/json':
// We'll use the inputstream module if it's installed because
// otherwise it's only possible to read the input stream once.
// And other parts of services or drupal might want to access it.
if (module_exists('inputstream')) {
$body = file_get_contents('drupal://input');
}
else {
$body = file_get_contents('php://input');
}
$in = json_decode($body, TRUE);
break;
// It's not included in the JSON-RPC standard but we support
// sending only the params as JSON.
default:
$in = $_POST;
$in['params'] = json_decode($in['params'], TRUE);
break;
}
break;
case 'GET':
// This handling of get requests doesn't implement the JSON-RPC spec
// fully. Multiple parameters sharing names are not collapsed into
// arrays. See
// http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#GetProcedureCall
// for details.
// TODO: Implement custom GET-parameter parsing
$in = array(
'jsonrpc' => '1.1',
'method' => basename($_GET['q']),
'params' => $_GET,
);
break;
}
$in['method'] = trim($in['method'], ' "');
$server = new JsonRpcServer($in);
try {
return $server->handle();
} catch (ServicesException $e) {
return $server->response($e->getData());
}
}
function jsonrpc_server_add_javascript() {
$path = drupal_get_path("module", "jsonrpc_server");
drupal_add_js($path ."/jsonrpc_server.js");
}