Lines Matching refs:self

134     def server_bind(self):
136 socketserver.TCPServer.server_bind(self)
137 host, port = self.server_address[:2]
138 self.server_name = socket.getfqdn(host)
139 self.server_port = port
267 def parse_request(self):
270 The request should be stored in self.raw_requestline; the results
271 are in self.command, self.path, self.request_version and
272 self.headers.
278 self.command = None # set in case of error on the first line
279 self.request_version = version = self.default_request_version
280 self.close_connection = True
281 requestline = str(self.raw_requestline, 'iso-8859-1')
283 self.requestline = requestline
309 self.send_error(
313 if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1":
314 self.close_connection = False
316 self.send_error(
320 self.request_version = version
323 self.send_error(
329 self.close_connection = True
331 self.send_error(
335 self.command, self.path = command, path
341 if self.path.startswith('//'):
342 self.path = '/' + self.path.lstrip('/') # Reduce to a single /
346 self.headers = http.client.parse_headers(self.rfile,
347 _class=self.MessageClass)
349 self.send_error(
355 self.send_error(
362 conntype = self.headers.get('Connection', "")
364 self.close_connection = True
366 self.protocol_version >= "HTTP/1.1"):
367 self.close_connection = False
369 expect = self.headers.get('Expect', "")
371 self.protocol_version >= "HTTP/1.1" and
372 self.request_version >= "HTTP/1.1"):
373 if not self.handle_expect_100():
377 def handle_expect_100(self):
391 self.send_response_only(HTTPStatus.CONTINUE)
392 self.end_headers()
395 def handle_one_request(self):
404 self.raw_requestline = self.rfile.readline(65537)
405 if len(self.raw_requestline) > 65536:
406 self.requestline = ''
407 self.request_version = ''
408 self.command = ''
409 self.send_error(HTTPStatus.REQUEST_URI_TOO_LONG)
411 if not self.raw_requestline:
412 self.close_connection = True
414 if not self.parse_request():
417 mname = 'do_' + self.command
418 if not hasattr(self, mname):
419 self.send_error(
421 "Unsupported method (%r)" % self.command)
423 method = getattr(self, mname)
425 self.wfile.flush() #actually send the response if not already done.
428 self.log_error("Request timed out: %r", e)
429 self.close_connection = True
432 def handle(self):
434 self.close_connection = True
436 self.handle_one_request()
437 while not self.close_connection:
438 self.handle_one_request()
440 def send_error(self, code, message=None, explain=None):
459 shortmsg, longmsg = self.responses[code]
466 self.log_error("code %d, message %s", code, message)
467 self.send_response(code, message)
468 self.send_header('Connection', 'close')
480 content = (self.error_message_format % {
486 self.send_header("Content-Type", self.error_content_type)
487 self.send_header('Content-Length', str(len(body)))
488 self.end_headers()
490 if self.command != 'HEAD' and body:
491 self.wfile.write(body)
493 def send_response(self, code, message=None):
501 self.log_request(code)
502 self.send_response_only(code, message)
503 self.send_header('Server', self.version_string())
504 self.send_header('Date', self.date_time_string())
506 def send_response_only(self, code, message=None):
508 if self.request_version != 'HTTP/0.9':
510 if code in self.responses:
511 message = self.responses[code][0]
514 if not hasattr(self, '_headers_buffer'):
515 self._headers_buffer = []
516 self._headers_buffer.append(("%s %d %s\r\n" %
517 (self.protocol_version, code, message)).encode(
520 def send_header(self, keyword, value):
522 if self.request_version != 'HTTP/0.9':
523 if not hasattr(self, '_headers_buffer'):
524 self._headers_buffer = []
525 self._headers_buffer.append(
530 self.close_connection = True
532 self.close_connection = False
534 def end_headers(self):
536 if self.request_version != 'HTTP/0.9':
537 self._headers_buffer.append(b"\r\n")
538 self.flush_headers()
540 def flush_headers(self):
541 if hasattr(self, '_headers_buffer'):
542 self.wfile.write(b"".join(self._headers_buffer))
543 self._headers_buffer = []
545 def log_request(self, code='-', size='-'):
553 self.log_message('"%s" %s %s',
554 self.requestline, str(code), str(size))
556 def log_error(self, format, *args):
568 self.log_message(format, *args)
575 def log_message(self, format, *args):
597 (self.address_string(),
598 self.log_date_time_string(),
599 message.translate(self._control_char_table)))
601 def version_string(self):
603 return self.server_version + ' ' + self.sys_version
605 def date_time_string(self, timestamp=None):
611 def log_date_time_string(self):
616 day, self.monthname[month], year, hh, mm, ss)
625 def address_string(self):
628 return self.client_address[0]
667 def __init__(self, *args, directory=None, **kwargs):
670 self.directory = os.fspath(directory)
673 def do_GET(self):
675 f = self.send_head()
678 self.copyfile(f, self.wfile)
682 def do_HEAD(self):
684 f = self.send_head()
688 def send_head(self):
699 path = self.translate_path(self.path)
702 parts = urllib.parse.urlsplit(self.path)
705 self.send_response(HTTPStatus.MOVED_PERMANENTLY)
709 self.send_header("Location", new_url)
710 self.send_header("Content-Length", "0")
711 self.end_headers()
719 return self.list_directory(path)
720 ctype = self.guess_type(path)
727 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
732 self.send_error(HTTPStatus.NOT_FOUND, "File not found")
738 if ("If-Modified-Since" in self.headers
739 and "If-None-Match" not in self.headers):
743 self.headers["If-Modified-Since"])
760 self.send_response(HTTPStatus.NOT_MODIFIED)
761 self.end_headers()
765 self.send_response(HTTPStatus.OK)
766 self.send_header("Content-type", ctype)
767 self.send_header("Content-Length", str(fs[6]))
768 self.send_header("Last-Modified",
769 self.date_time_string(fs.st_mtime))
770 self.end_headers()
776 def list_directory(self, path):
787 self.send_error(
794 displaypath = urllib.parse.unquote(self.path,
797 displaypath = urllib.parse.unquote(self.path)
827 self.send_response(HTTPStatus.OK)
828 self.send_header("Content-type", "text/html; charset=%s" % enc)
829 self.send_header("Content-Length", str(len(encoded)))
830 self.end_headers()
833 def translate_path(self, path):
853 path = self.directory
863 def copyfile(self, source, outputfile):
879 def guess_type(self, path):
888 up in the table self.extensions_map, using application/octet-stream
894 if ext in self.extensions_map:
895 return self.extensions_map[ext]
897 if ext in self.extensions_map:
898 return self.extensions_map[ext]
995 def do_POST(self):
1002 if self.is_cgi():
1003 self.run_cgi()
1005 self.send_error(
1009 def send_head(self):
1011 if self.is_cgi():
1012 return self.run_cgi()
1014 return SimpleHTTPRequestHandler.send_head(self)
1016 def is_cgi(self):
1017 """Test whether self.path corresponds to a CGI script.
1020 (dir, rest) if self.path requires running a CGI script.
1024 self.path was rejected as invalid and act accordingly.
1027 path begins with one of the strings in self.cgi_directories
1031 collapsed_path = _url_collapse_path(self.path)
1033 while dir_sep > 0 and not collapsed_path[:dir_sep] in self.cgi_directories:
1037 self.cgi_info = head, tail
1044 def is_executable(self, path):
1048 def is_python(self, path):
1053 def run_cgi(self):
1055 dir, rest = self.cgi_info
1062 scriptdir = self.translate_path(nextdir)
1081 scriptfile = self.translate_path(scriptname)
1083 self.send_error(
1088 self.send_error(
1092 ispy = self.is_python(scriptname)
1093 if self.have_fork or not ispy:
1094 if not self.is_executable(scriptfile):
1095 self.send_error(
1103 env['SERVER_SOFTWARE'] = self.version_string()
1104 env['SERVER_NAME'] = self.server.server_name
1106 env['SERVER_PROTOCOL'] = self.protocol_version
1107 env['SERVER_PORT'] = str(self.server.server_port)
1108 env['REQUEST_METHOD'] = self.command
1111 env['PATH_TRANSLATED'] = self.translate_path(uqrest)
1114 env['REMOTE_ADDR'] = self.client_address[0]
1115 authorization = self.headers.get("authorization")
1133 if self.headers.get('content-type') is None:
1134 env['CONTENT_TYPE'] = self.headers.get_content_type()
1136 env['CONTENT_TYPE'] = self.headers['content-type']
1137 length = self.headers.get('content-length')
1140 referer = self.headers.get('referer')
1143 accept = self.headers.get_all('accept', ())
1145 ua = self.headers.get('user-agent')
1148 co = filter(None, self.headers.get_all('cookie', []))
1159 self.send_response(HTTPStatus.OK, "Script output follows")
1160 self.flush_headers()
1164 if self.have_fork:
1170 self.wfile.flush() # Always flush before forking
1176 while select.select([self.rfile], [], [], 0)[0]:
1177 if not self.rfile.read(1):
1181 self.log_error(f"CGI script exit code {exitcode}")
1189 os.dup2(self.rfile.fileno(), 0)
1190 os.dup2(self.wfile.fileno(), 1)
1193 self.server.handle_error(self.request, self.client_address)
1200 if self.is_python(scriptfile):
1208 self.log_message("command: %s", subprocess.list2cmdline(cmdline))
1219 if self.command.lower() == "post" and nbytes > 0:
1220 data = self.rfile.read(nbytes)
1224 while select.select([self.rfile._sock], [], [], 0)[0]:
1225 if not self.rfile._sock.recv(1):
1228 self.wfile.write(stdout)
1230 self.log_error('%s', stderr)
1235 self.log_error("CGI script exit status %#x", status)
1237 self.log_message("CGI script exited OK")
1302 def server_bind(self):
1305 self.socket.setsockopt(
1309 def finish_request(self, request, client_address):
1310 self.RequestHandlerClass(request, client_address, self,