Skip to content

Commit 134fd35

Browse files
committed
use JSON payload to post data to Eventum
this way we don't run into payload size issues
1 parent d078c28 commit 134fd35

File tree

2 files changed

+70
-21
lines changed

2 files changed

+70
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This version of hooks require Eventum 3.1.0.
88
From this version onwards eventum-scm is not released as part of main Eventum release.
99

1010
- cvs/svn/git hooks rewritten to handle new payload for Eventum 3.1.0
11+
- use JSON payload to post data to Eventum
1112

1213
2016-04-19, Version [3.0.12]
1314
----------------------------

helpers.php

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,20 @@ function scm_ping($params)
5050
{
5151
global $PROGRAM, $eventum_url;
5252

53-
$ping_url = $eventum_url . 'scm_ping.php';
54-
$params['json'] = 1;
53+
$ping_url = $eventum_url . 'scm_ping.php?scm='.$params['scm'];
54+
$status = json_post($ping_url, $params, 1);
5555

56-
$res = wget($ping_url, $params);
57-
if (!$res) {
58-
throw new RuntimeException("Couldn't read response from $ping_url");
56+
if ($status['code']) {
57+
throw new RuntimeException($status['message'], $status['code']);
5958
}
6059

61-
list($headers, $data) = $res;
62-
// status line is first header in response
63-
$status = array_shift($headers);
64-
list($proto, $status, $msg) = explode(' ', trim($status), 3);
65-
if ($status != '200') {
66-
throw new RuntimeException("Could not ping the Eventum SCM handler script: HTTP status code: $status $msg");
67-
}
68-
69-
$status = json_decode($data, true);
70-
// if response is json, try to figure error from there
71-
if (is_array($status)) {
72-
if ($status['code']) {
73-
throw new RuntimeException($status['message'], $status['code']);
74-
}
75-
$data = $status['message'];
60+
$message = trim($status['message']);
61+
if (!$message) {
62+
return;
7663
}
7764

7865
// prefix response with our name
79-
foreach (explode("\n", trim($data)) as $line) {
66+
foreach (explode("\n", $message) as $line) {
8067
echo "$PROGRAM: $line\n";
8168
}
8269
}
@@ -165,3 +152,64 @@ function wget($url, $params, $headers = true)
165152

166153
return $data;
167154
}
155+
156+
/**
157+
* POST json encoded data to $url
158+
*
159+
* @param string $url
160+
* @param array $data
161+
* @param bool $assoc
162+
* @return array|stdClass result with extra 'meta' key
163+
* @author Elan Ruusamäe <glen@delfi.ee>
164+
*/
165+
function json_post($url, $data, $assoc = false)
166+
{
167+
// see if schema in url is supported
168+
$scheme = parse_url($url, PHP_URL_SCHEME);
169+
if (!in_array($scheme, stream_get_wrappers())) {
170+
throw new RuntimeException("$scheme:// scheme not supported. Load openssl php extension?");
171+
}
172+
173+
$body = json_encode($data);
174+
$headers = array(
175+
'Expect: ',
176+
'Content-Type: application/json',
177+
'Accept: application/json',
178+
'Content-Length: ' . strlen($body),
179+
);
180+
181+
$options = array(
182+
'method' => 'POST',
183+
'content' => $body,
184+
'header' => implode("\r\n", $headers)
185+
);
186+
$options = array(
187+
$scheme => $options
188+
);
189+
190+
$context = stream_context_create($options);
191+
192+
$stream = @fopen($url, 'r', false, $context);
193+
if (!$stream) {
194+
$error = error_get_last();
195+
throw new RuntimeException($error['message']);
196+
}
197+
198+
$meta = stream_get_meta_data($stream);
199+
$result = stream_get_contents($stream);
200+
fclose($stream);
201+
202+
$response = json_decode($result, $assoc);
203+
if (!$response) {
204+
throw new InvalidArgumentException("Unable to decode: $result");
205+
}
206+
if ($assoc) {
207+
$response['meta'] = $meta;
208+
$response['raw'] = $result;
209+
} else {
210+
$response->raw = $result;
211+
$response->meta = $meta;
212+
}
213+
214+
return $response;
215+
}

0 commit comments

Comments
 (0)