forked from github/github-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
63 lines (51 loc) · 1.63 KB
/
web.rb
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
class Service::Web < Service
HMAC_DIGEST = OpenSSL::Digest::Digest.new('sha1')
string :url,
# adds a X-Hub-Signature of the body content
# X-Hub-Signature: sha1=....
:secret,
# old hooks send form params ?payload=JSON(...)
# new hooks should set content_type == 'json'
:content_type
boolean :insecure_ssl # :(
def receive_event
url = data['url'].to_s
url.gsub! /\s/, ''
if url.empty?
raise_config_error "Invalid URL: #{url.inspect}"
end
if url !~ /^https?\:\/\//
url = "http://#{url}"
end
# set this so that basic auth is added,
# and GET params are added to the POST body
http.url_prefix = url
http.headers['X-GitHub-Event'] = event.to_s
if data['insecure_ssl'].to_i == 1
http.ssl[:verify] = false
end
body = if data['content_type'] == 'json'
http.headers['content-type'] = 'application/json'
JSON.generate(payload)
else
http.headers['content-type'] = 'application/x-www-form-urlencoded'
Faraday::Utils.build_nested_query(
http.params.merge(:payload => JSON.generate(payload)))
end
if !(secret = data['secret'].to_s).empty?
http.headers['X-Hub-Signature'] =
'sha1='+OpenSSL::HMAC.hexdigest(HMAC_DIGEST, secret, body)
end
http_post url, body
rescue Addressable::URI::InvalidURIError, Errno::EHOSTUNREACH
raise_config_error $!.to_s
rescue SocketError
if $!.to_s =~ /getaddrinfo:/
raise_config_error "Invalid host name."
else
raise
end
rescue EOFError
raise_config_error "Invalid server response. Make sure the URL uses the correct protocol."
end
end