4 # Copyright (C) 2007, 2008 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """HTTP client module.
25 # pylint: disable-msg=E1103
27 # # E1103: %s %r has no %r member (but some types could not be
28 # inferred), since _socketobject could be ssl or not and pylint
38 from ganeti import workerpool
39 from ganeti import http
42 HTTP_CLIENT_THREADS = 10
45 class HttpClientRequest(object):
46 def __init__(self, host, port, method, path, headers=None, post_data=None,
47 ssl_params=None, ssl_verify_peer=False):
48 """Describes an HTTP request.
55 @param method: Method name
57 @param path: Request path
58 @type headers: dict or None
59 @param headers: Additional headers to send
60 @type post_data: string or None
61 @param post_data: Additional data to send
62 @type ssl_params: HttpSslParams
63 @param ssl_params: SSL key and certificate
64 @type ssl_verify_peer: bool
65 @param ssl_verify_peer: Whether to compare our certificate with
69 if post_data is not None:
70 assert method.upper() in (http.HTTP_POST, http.HTTP_PUT), \
71 "Only POST and GET requests support sending data"
73 assert path.startswith("/"), "Path must start with slash (/)"
78 self.ssl_params = ssl_params
79 self.ssl_verify_peer = ssl_verify_peer
82 self.headers = headers
83 self.post_data = post_data
92 self.resp_version = None
93 self.resp_status_code = None
94 self.resp_reason = None
95 self.resp_headers = None
99 status = ["%s.%s" % (self.__class__.__module__, self.__class__.__name__),
100 "%s:%s" % (self.host, self.port),
104 return "<%s at %#x>" % (" ".join(status), id(self))
107 class _HttpClientToServerMessageWriter(http.HttpMessageWriter):
111 class _HttpServerToClientMessageReader(http.HttpMessageReader):
113 START_LINE_LENGTH_MAX = 512
114 HEADER_LENGTH_MAX = 4096
116 def ParseStartLine(self, start_line):
117 """Parses the status line sent by the server.
120 # Empty lines are skipped when reading
124 [version, status, reason] = start_line.split(None, 2)
127 [version, status] = start_line.split(None, 1)
130 version = http.HTTP_0_9
133 version = version.upper()
135 # The status code is a three-digit number
138 if status < 100 or status > 999:
140 except (TypeError, ValueError):
144 raise http.HttpError("Invalid status code (%r)" % start_line)
146 return http.HttpServerToClientStartLine(version, status, reason)
149 class HttpClientRequestExecutor(http.HttpBase):
152 http.HTTP_USER_AGENT: http.HTTP_GANETI_VERSION,
153 # TODO: For keep-alive, don't send "Connection: close"
154 http.HTTP_CONNECTION: "close",
157 # Timeouts in seconds for socket layer
158 # TODO: Soft timeout instead of only socket timeout?
159 # TODO: Make read timeout configurable per OpCode?
165 def __init__(self, req):
166 """Initializes the HttpClientRequestExecutor class.
168 @type req: HttpClientRequest
169 @param req: Request object
172 http.HttpBase.__init__(self)
176 # TODO: Implement connection caching/keep-alive
177 self.sock = self._CreateSocket(req.ssl_params,
180 # Disable Python's timeout
181 self.sock.settimeout(None)
183 # Operate in non-blocking mode
184 self.sock.setblocking(0)
186 response_msg_reader = None
193 (response_msg_reader, response_msg) = self._ReadResponse()
195 # Only wait for server to close if we didn't have any exception.
198 # TODO: Keep-alive is not supported, always close connection
200 http.ShutdownConnection(self.sock, self.CLOSE_TIMEOUT,
201 self.WRITE_TIMEOUT, response_msg_reader,
207 req.response = response_msg
209 req.resp_version = req.response.start_line.version
210 req.resp_status_code = req.response.start_line.code
211 req.resp_reason = req.response.start_line.reason
212 req.resp_headers = req.response.headers
213 req.resp_body = req.response.body
218 except http.HttpError, err:
223 """Non-blocking connect to host with timeout.
229 connect_error = self.sock.connect_ex((self.request.host,
231 except socket.gaierror, err:
232 raise http.HttpError("Connection failed: %s" % str(err))
234 if connect_error == errno.EINTR:
238 elif connect_error == 0:
239 # Connection established
243 elif connect_error == errno.EINPROGRESS:
247 raise http.HttpError("Connection failed (%s: %s)" %
248 (connect_error, os.strerror(connect_error)))
251 # Wait for connection
252 event = http.WaitForSocketCondition(self.sock, select.POLLOUT,
253 self.CONNECT_TIMEOUT)
255 raise http.HttpError("Timeout while connecting to server")
258 connect_error = self.sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
259 if connect_error != 0:
260 raise http.HttpError("Connection failed (%s: %s)" %
261 (connect_error, os.strerror(connect_error)))
263 # Enable TCP keep-alive
264 self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
266 # If needed, Linux specific options are available to change the TCP
267 # keep-alive settings, see "man 7 tcp" for TCP_KEEPCNT, TCP_KEEPIDLE and
270 # Do the secret SSL handshake
272 self.sock.set_connect_state() # pylint: disable-msg=E1103
274 http.Handshake(self.sock, self.WRITE_TIMEOUT)
275 except http.HttpSessionHandshakeUnexpectedEOF:
276 raise http.HttpError("Server closed connection during SSL handshake")
278 def _SendRequest(self):
279 """Sends request to server.
283 send_headers = self.DEFAULT_HEADERS.copy()
285 if self.request.headers:
286 send_headers.update(self.request.headers)
288 send_headers[http.HTTP_HOST] = "%s:%s" % (self.request.host,
292 msg = http.HttpMessage()
294 # Combine request line. We only support HTTP/1.0 (no chunked transfers and
296 # TODO: For keep-alive, change to HTTP/1.1
298 http.HttpClientToServerStartLine(method=self.request.method.upper(),
299 path=self.request.path,
300 version=http.HTTP_1_0)
301 msg.headers = send_headers
302 msg.body = self.request.post_data
305 _HttpClientToServerMessageWriter(self.sock, msg, self.WRITE_TIMEOUT)
306 except http.HttpSocketTimeout:
307 raise http.HttpError("Timeout while sending request")
308 except socket.error, err:
309 raise http.HttpError("Error sending request: %s" % err)
311 def _ReadResponse(self):
312 """Read response from server.
315 response_msg = http.HttpMessage()
318 response_msg_reader = \
319 _HttpServerToClientMessageReader(self.sock, response_msg,
321 except http.HttpSocketTimeout:
322 raise http.HttpError("Timeout while reading response")
323 except socket.error, err:
324 raise http.HttpError("Error reading response: %s" % err)
326 return (response_msg_reader, response_msg)
329 class _HttpClientPendingRequest(object):
330 """Data class for pending requests.
333 def __init__(self, request):
334 self.request = request
336 # Thread synchronization
337 self.done = threading.Event()
340 status = ["%s.%s" % (self.__class__.__module__, self.__class__.__name__),
341 "req=%r" % self.request]
343 return "<%s at %#x>" % (" ".join(status), id(self))
346 class HttpClientWorker(workerpool.BaseWorker):
347 """HTTP client worker class.
350 def RunTask(self, pend_req): # pylint: disable-msg=W0221
352 HttpClientRequestExecutor(pend_req.request)
357 class HttpClientWorkerPool(workerpool.WorkerPool):
358 def __init__(self, manager):
359 workerpool.WorkerPool.__init__(self, "HttpClient",
362 self.manager = manager
365 class HttpClientManager(object):
366 """Manages HTTP requests.
370 self._wpool = HttpClientWorkerPool(self)
375 def ExecRequests(self, requests):
376 """Execute HTTP requests.
378 This function can be called from multiple threads at the same time.
380 @type requests: List of HttpClientRequest instances
381 @param requests: The requests to execute
382 @rtype: List of HttpClientRequest instances
383 @return: The list of requests passed in
386 # _HttpClientPendingRequest is used for internal thread synchronization
387 pending = [_HttpClientPendingRequest(req) for req in requests]
390 # Add requests to queue
391 for pend_req in pending:
392 self._wpool.AddTask(pend_req)
395 # In case of an exception we should still wait for the rest, otherwise
396 # another thread from the worker pool could modify the request object
399 # And wait for them to finish
400 for pend_req in pending:
403 # Return original list
407 self._wpool.Quiesce()
408 self._wpool.TerminateWorkers()