Пример #1
0
 def run_domain1(self, sock):
     while True:
         req = sock.recv_request()
         if req is None:
             break
         if req.method == "GET":
             response = messages.Response(req, entity_body=TEST_STRING)
             response.set_status(200, "You got it!")
         elif req.method == "HEAD":
             response = messages.Response(req)
             response.set_content_length(len(TEST_STRING))
             response.set_status(200, "You got it!")
         elif req.method == "PUT":
             # check the request body
             response = messages.Response(req, entity_body='')
             if req.entity_body.getvalue() != TEST_BODY:
                 response.set_status(400, "PUT failed for domain1")
                 logging.debug("run_domain1: PUT %s" %
                               repr(req.entity_body.getvalue()))
             else:
                 response.set_status(200)
         else:
             response = messages.Response(req)
             response.set_status(400, "Test failed for domain1")
         sock.send_response(response)
Пример #2
0
 def run_domain5(self, sock):
     while True:
         req = sock.recv_request()
         if req is None:
             break
         if self.unreliable:
             self.unreliable = False
             # shutdown the socket
             logging.debug("Server hang-up after reading request")
             sock.mock_shutdown(socket.SHUT_RDWR)
             break
         if req.method == "GET":
             response = messages.Response(req, entity_body=TEST_STRING)
             response.set_status(200, "Thanks for your patience")
         elif req.method == "HEAD":
             response = messages.Response(req)
             response.set_content_length(len(TEST_STRING))
             response.set_status(200, "Thanks for your patience")
         elif req.method == "POST":
             response = messages.Response(req, entity_body=TEST_STRING)
             response.set_content_length(len(TEST_STRING))
             response.set_status(200, "Thanks for your patience")
         else:
             response = messages.Response(req)
             response.set_status(400, "Test failed for domain5")
         sock.send_response(response)
Пример #3
0
 def run_domain2(self, sock):
     while True:
         req = sock.recv_request()
         if req is None:
             break
         if req.method in ("HEAD", "GET"):
             response = messages.Response(req, entity_body=None)
             response.set_status(301, "Moved")
             response.set_location("http://www.domain1.com/")
         else:
             response = messages.Response(req)
             response.set_status(400, "Test failed for domain2")
         sock.send_response(response)
Пример #4
0
 def run_domain2(self, sock):
     negotiate = NegotiateChallenge()
     while True:
         req = sock.recv_request()
         if req is None:
             break
         if req.method == "GET":
             # check the authorization header
             logging.debug("Received headers: %s", repr(req.headers))
             credentials = req.get_authorization()
             logging.debug("Parsed Authorization: %s", str(credentials))
             if (isinstance(credentials, NTLMParsedCredentials)):
                 if credentials.msg[8] == byte(1) and self.ntlm_state == 1:
                     # negotiate message
                     data = mock_challenge()
                     challenge = NTLMChallenge(base64.b64encode(data))
                     response = messages.Response(req)
                     response.set_status(401, "Who are you?")
                     # response.set_content_length(0)
                     response.set_www_authenticate([negotiate, challenge])
                     self.ntlm_state = 3
                 elif credentials.msg[8] == byte(3) and \
                         self.ntlm_state == 3:
                     # authenticate message
                     response = messages.Response(req,
                                                  entity_body=TEST_STRING)
                     response.set_status(200, "You got it!")
                     self.ntlm_state = 0
                 else:
                     response.set_status(403, "Who are you?")
                     # response.set_content_length(0)
                     self.ntlm_state = 0
             else:
                 challenge = NTLMChallenge()
                 response = messages.Response(req)
                 response.set_status(401, "Who are you?")
                 # response.set_content_length(0)
                 response.set_www_authenticate([negotiate, challenge])
                 self.ntlm_state = 1
         else:
             response = messages.Response(req)
             response.set_status(400, "Test failed for domain1")
             self.ntlm_state = 1
         sock.send_response(response)
Пример #5
0
 def run_domain9(self, sock):
     while True:
         # simulates a server that supports upgrade to happy
         req = sock.recv_request()
         if req is None:
             break
         connection = req.get_connection()
         if "upgrade" in connection:
             response = messages.Response(req)
             response.set_status(101)
             response.set_upgrade([params.ProductToken("happy")])
             sock.send_response(response)
             logging.debug("Switching to happy protocol")
             input = sock.send_pipe.readmatch()
             sock.recv_pipe.write(input)
             sock.recv_pipe.write_eof()
         else:
             response = messages.Response(req)
             response.set_status(400, "Test failed for domain9")
             sock.send_response(response)
         sock.mock_shutdown(socket.SHUT_RDWR)
         break
Пример #6
0
 def run_domain1(self, sock):
     while True:
         req = sock.recv_request()
         if req is None:
             break
         if req.method == "GET":
             # check the authorization header
             credentials = req.get_authorization()
             if (isinstance(credentials, BasicCredentials)
                     and credentials.userid == "user"
                     and credentials.password == "Password"):
                 response = messages.Response(req, entity_body=TEST_STRING)
                 response.set_status(200, "You got it!")
             else:
                 challenge = BasicChallenge(('realm', b"RFC2617", True))
                 response = messages.Response(req)
                 response.set_status(401, "Who are you?")
                 # response.set_content_length(0)
                 response.set_www_authenticate([challenge])
         else:
             response = messages.Response(req)
             response.set_status(400, "Test failed for domain1")
         sock.send_response(response)
Пример #7
0
 def run_domain8(self, sock):
     while True:
         # simulates a server with a short fuse, shuts down
         # the socket after a single request
         req = sock.recv_request()
         if req is None:
             break
         if req.method == "GET":
             response = messages.Response(req, entity_body=TEST_STRING)
             response.set_status(200, "Success")
         elif req.method == "HEAD":
             response = messages.Response(req)
             response.set_content_length(len(TEST_STRING))
             response.set_status(200, "Success")
         elif req.method == "POST":
             response = messages.Response(req)
             response.set_status(200, "Success")
         else:
             response = messages.Response(req)
             response.set_status(400, "Test failed for domain8")
         sock.send_response(response)
         logging.debug("Server hang-up after 0s idle time")
         sock.mock_shutdown(socket.SHUT_RDWR)
         break
Пример #8
0
 def receive_response(self, request):
     response = messages.Response(request)
     response.start_receiving()
     while True:
         mode = response.recv_mode()
         if mode == messages.Message.RECV_LINE:
             response.recv(self.send_pipe.readmatch())
         elif mode == messages.Message.RECV_HEADERS:
             lines = []
             last_line = b''
             while last_line != b'\r\n':
                 last_line = self.send_pipe.readmatch()
                 lines.append(last_line)
             response.recv(lines)
         elif mode is None:
             break
         elif mode > 0:
             response.recv(self.send_pipe.read(mode))
         elif mode == messages.Message.RECV_ALL:
             response.recv(self.send_pipe.read())
         else:
             raise ValueError("unexpected recv_mode!")
     return response
Пример #9
0
 def run_domain7(self, sock):
     while True:
         if self.error_count > 1:
             # generate an error on the socket for the client
             sock.io_error = socket.error(errno.ENOLINK,
                                          os.strerror(errno.ENOLINK))
             self.error_count -= 1
             break
         elif self.error_count:
             req = sock.recv_request()
             if req is None:
                 break
             sock.io_error = socket.error(errno.ENOLINK,
                                          os.strerror(errno.ENOLINK))
             self.error_count -= 1
             sock.close()
             break
         else:
             req = sock.recv_request()
             if req is None:
                 break
             response = messages.Response(req, entity_body=TEST_STRING)
             response.set_status(200, "Thanks for your patience")
         sock.send_response(response)