Skip to content

Commit 87b20e9

Browse files
author
Vladimir Kotal
committed
split opengrok into webtuil
allow headers/body in http requests to be empty fixes #2383
1 parent 3677e50 commit 87b20e9

File tree

3 files changed

+96
-70
lines changed

3 files changed

+96
-70
lines changed

tools/src/main/python/commands.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2222
#
2323

2424
import logging
2525
from command import Command
26-
from opengrok import put, post, delete
26+
from webutil import put, post, delete
2727
from utils import is_web_uri
2828
import json
2929

@@ -95,6 +95,8 @@ def call_rest_api(self, command):
9595
verb = command[1]
9696
data = command[2]
9797

98+
headers = None
99+
json_data = None
98100
if len(data) > 0:
99101
headers = {'Content-Type': 'application/json'}
100102
json_data = json.dumps(data).replace(self.PROJECT_SUBST, self.name)

tools/src/main/python/opengrok.py

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,8 @@
2121
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2222
#
2323

24-
import requests
2524
import urllib.parse
26-
import traceback
27-
from urllib.parse import urlparse
28-
29-
30-
def get(logger, uri, params=None, headers=None):
31-
try:
32-
proxies = get_proxies(uri)
33-
return requests.get(uri, params=params, proxies=proxies)
34-
except Exception:
35-
logger.debug(traceback.format_exc())
36-
return None
37-
38-
39-
def delete(logger, uri, params=None, headers=None):
40-
try:
41-
proxies = get_proxies(uri)
42-
return requests.delete(uri, params=params, proxies=proxies)
43-
except Exception:
44-
logger.debug(traceback.format_exc())
45-
return None
46-
47-
48-
def post(logger, uri, headers=None, data=None):
49-
rv = None
50-
try:
51-
proxies = get_proxies(uri)
52-
rv = requests.post(uri, data=data, headers=headers, proxies=proxies)
53-
except Exception:
54-
logger.debug(traceback.format_exc())
55-
return None
56-
57-
return rv
58-
59-
60-
def put(logger, uri, headers=None, data=None):
61-
rv = None
62-
try:
63-
proxies = get_proxies(uri)
64-
rv = requests.put(uri, data=data, headers=headers, proxies=proxies)
65-
except Exception:
66-
logger.debug(traceback.format_exc())
67-
return None
68-
69-
return rv
25+
from webutil import put, get, post, delete, get_uri
7026

7127

7228
def get_repos(logger, project, uri):
@@ -181,26 +137,3 @@ def delete_project(logger, project, uri):
181137
return False
182138

183139
return True
184-
185-
186-
def get_uri(*uri_parts):
187-
return '/'.join(s.strip('/') for s in uri_parts)
188-
189-
190-
def is_localhost_url(url):
191-
"""
192-
Check if given URL is based on localhost.
193-
"""
194-
195-
o = urlparse(url)
196-
return o.hostname in ['localhost', '127.0.0.1', '::1']
197-
198-
199-
def get_proxies(url):
200-
"""
201-
For localhost based requests it is undesirable to use proxies.
202-
"""
203-
if is_localhost_url(url):
204-
return {'http': None, 'https': None}
205-
else:
206-
return None

tools/src/main/python/webutil.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#
2+
# CDDL HEADER START
3+
#
4+
# The contents of this file are subject to the terms of the
5+
# Common Development and Distribution License (the "License").
6+
# You may not use this file except in compliance with the License.
7+
#
8+
# See LICENSE.txt included in this distribution for the specific
9+
# language governing permissions and limitations under the License.
10+
#
11+
# When distributing Covered Code, include this CDDL HEADER in each
12+
# file and include the License file at LICENSE.txt.
13+
# If applicable, add the following below this CDDL HEADER, with the
14+
# fields enclosed by brackets "[]" replaced with your own identifying
15+
# information: Portions Copyright [yyyy] [name of copyright owner]
16+
#
17+
# CDDL HEADER END
18+
#
19+
20+
#
21+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
22+
#
23+
24+
import requests
25+
import traceback
26+
from urllib.parse import urlparse
27+
28+
29+
def get(logger, uri, params=None, headers=None):
30+
try:
31+
proxies = get_proxies(uri)
32+
return requests.get(uri, params=params, proxies=proxies)
33+
except Exception:
34+
logger.debug(traceback.format_exc())
35+
return None
36+
37+
38+
def delete(logger, uri, params=None, headers=None):
39+
try:
40+
proxies = get_proxies(uri)
41+
return requests.delete(uri, params=params, proxies=proxies)
42+
except Exception:
43+
logger.debug(traceback.format_exc())
44+
return None
45+
46+
47+
def post(logger, uri, headers=None, data=None):
48+
rv = None
49+
try:
50+
proxies = get_proxies(uri)
51+
rv = requests.post(uri, data=data, headers=headers, proxies=proxies)
52+
except Exception:
53+
logger.debug(traceback.format_exc())
54+
return None
55+
56+
return rv
57+
58+
59+
def put(logger, uri, headers=None, data=None):
60+
rv = None
61+
try:
62+
proxies = get_proxies(uri)
63+
rv = requests.put(uri, data=data, headers=headers, proxies=proxies)
64+
except Exception:
65+
logger.debug(traceback.format_exc())
66+
return None
67+
68+
return rv
69+
70+
71+
def get_uri(*uri_parts):
72+
return '/'.join(s.strip('/') for s in uri_parts)
73+
74+
75+
def is_localhost_url(url):
76+
"""
77+
Check if given URL is based on localhost.
78+
"""
79+
80+
o = urlparse(url)
81+
return o.hostname in ['localhost', '127.0.0.1', '::1']
82+
83+
84+
def get_proxies(url):
85+
"""
86+
For localhost based requests it is undesirable to use proxies.
87+
"""
88+
if is_localhost_url(url):
89+
return {'http': None, 'https': None}
90+
else:
91+
return None

0 commit comments

Comments
 (0)