-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create proxy mapper to extract the mapping logic to its own class
Adding watchdog to watch the folder and rebuild the mapping index
- Loading branch information
Omar Abdelhafith
committed
Jun 28, 2015
1 parent
b1e6f70
commit c894e07
Showing
11 changed files
with
177 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/env python | ||
# -*- encoding: utf-8 -*- | ||
|
||
import httplib | ||
from threading import Thread | ||
|
||
from libmproxy.protocol.http import HTTPRequest, HTTPResponse | ||
from netlib.odict import ODictCaseless | ||
|
||
import mockpy.utils.proxy_extensions | ||
from ..utils.config import * | ||
from mockpy.status.status import Status | ||
from ..utils import log | ||
from ..models.mapping_items_manager import * | ||
|
||
class ProxyMapper(object): | ||
|
||
def __init__(self, mapping_handler, http_proxy): | ||
self.http_proxy = http_proxy | ||
self.mapping_handler = mapping_handler | ||
self.status = Status(self.mapping_handler) | ||
|
||
success("Proxy server started") | ||
|
||
def handle_request(self, flow): | ||
log.log_url(flow.request.url) | ||
|
||
if self.status.is_status(flow.request.url): | ||
info("Accessing Satus") | ||
flow.reply(HTTPResponse.with_html(self.status.html_response())) | ||
log.print_seperator() | ||
return | ||
|
||
request = flow.request.to_mapper_request() | ||
mapping_items = self.mapping_handler.mapping_item_for_mapping_request(request) | ||
|
||
if len(mapping_items) > 1: | ||
log.log_multiple_matches(mapping_items) | ||
|
||
if len(mapping_items) == 0: | ||
self.perform_http_request(flow) | ||
else: | ||
self.perform_mapping_request(flow, mapping_items[0]) | ||
|
||
log.print_seperator() | ||
|
||
def perform_mapping_request(self, flow, mapping_item): | ||
response, request = mapping_item.response, mapping_item.request | ||
|
||
log.log_request(request) | ||
log.log_response(response) | ||
|
||
response = HTTPResponse.from_intercepted_response(response) | ||
flow.reply(response) | ||
|
||
def perform_http_request(self, flow): | ||
if self.http_proxy is None: | ||
flow.reply() | ||
else: | ||
thread = Thread(target=self.threaded_perform_http_request, | ||
args=(flow, self.http_proxy)) | ||
thread.start() | ||
|
||
def threaded_perform_http_request(self, flow, proxy_settings): | ||
response = self.perform_http_connection(flow.request, proxy_settings[0], proxy_settings[1]) | ||
flow.reply(response) | ||
|
||
@staticmethod | ||
def perform_http_connection(request, url, port): | ||
try: | ||
conn = httplib.HTTPConnection(url, port) | ||
headers = dict(request.headers.items()) | ||
|
||
conn.request(request.method, request.url, | ||
body=request.content, headers=headers) | ||
httplib_response = conn.getresponse() | ||
|
||
headers = ODictCaseless.from_httplib_headers(httplib_response.getheaders()) | ||
response = HTTPResponse(code=httplib_response.status, | ||
content=httplib_response.read(), | ||
msg="", | ||
httpversion=(1, 1), | ||
headers=headers) | ||
return response | ||
except Exception as ex: | ||
error("Error Happened") | ||
error(ex) | ||
error("method: %s\nurl: %s\nbody: --\nheaders: --" % | ||
(request.method, request.url)) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,65 @@ | ||
__author__ = 'omarsubhiabdelhafith' | ||
|
||
from .mapping_item import * | ||
from mockpy.utils import log | ||
from functools import partial | ||
import re | ||
from watchdog.observers import Observer | ||
from watchdog.events import FileSystemEventHandler | ||
|
||
class MappingItemsManager(object): | ||
|
||
def __init__(self, inout_path, res_path): | ||
self.yaml_files = get_yaml_files(inout_path) | ||
create_mapping_item_with_yaml = partial(create_mapping_item, inout_path, res_path) | ||
self.mappings = list(map(create_mapping_item_with_yaml, self.yaml_files)) | ||
self.inout_path = inout_path | ||
self.res_path = res_path | ||
self.parse_inout_and_res() | ||
self.install_watchers() | ||
|
||
def parse_inout_and_res(self): | ||
self.yaml_files = get_yaml_files(self.inout_path) | ||
self.mappings = list(map(self.create_mapping_item, self.yaml_files)) | ||
|
||
def create_mapping_item(self, yml_file): | ||
full_path = self.inout_path + "/" + yml_file | ||
|
||
with open(full_path, "r") as file: | ||
return MappingItem(yaml.load(file), full_path, self.res_path) | ||
|
||
def response_for_mapping_request(self, request): | ||
return [item.response for item in self.mappings if item.handles_mapping_request(request)] | ||
|
||
def mapping_item_for_mapping_request(self, request): | ||
return [item for item in self.mappings if item.handles_mapping_request(request)] | ||
|
||
def install_watchers(self): | ||
self.event_handler = MapperDirectoryListener(self) | ||
self.install_watcher(self.inout_path) | ||
self.install_watcher(self.res_path) | ||
|
||
def install_watcher(self, path): | ||
observer = Observer() | ||
observer.schedule(self.event_handler, path, recursive=True) | ||
observer.start() | ||
|
||
|
||
class MapperDirectoryListener(FileSystemEventHandler): | ||
|
||
def __init__(self, mapping_manager): | ||
self.mapping_manager = mapping_manager | ||
|
||
def on_any_event(self, event): | ||
log.info("Inout or Res directory changed, rebuilding mapping settings\n" | ||
"File path: %s" % event.src_path + | ||
"\nEvent type: %s" % event.event_type) | ||
|
||
self.mapping_manager.parse_inout_and_res() | ||
log.success("Mapping settings rebuilt successfully") | ||
log.print_seperator() | ||
|
||
|
||
def get_yaml_files(path): | ||
files = os.listdir(path) | ||
return filter(lambda file: re.match(".*\.yml$", file), files) | ||
|
||
|
||
def create_mapping_item(inout_path, res_path, yml_file): | ||
file_to_open = inout_path + "/" + yml_file | ||
with open(file_to_open, "r") as file: | ||
return MappingItem(yaml.load(file), file_to_open, res_path) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import cherrypy | ||
from mockpy.models.mapping_request import * | ||
|
||
|
||
""" | ||
Extensions | ||
""" | ||
def to_mapper_request(): | ||
dic = {"method": cherrypy.request.method, | ||
"url": cherrypy.url(), | ||
"headers": cherrypy.request.headers} | ||
|
||
if cherrypy.request.process_request_body: | ||
dic["body"] = cherrypy.request.body.read() | ||
|
||
return MappingRequest(dic) | ||
|
||
setattr(cherrypy, "to_mapper_request", to_mapper_request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
netlib | ||
wheel==0.23.0 | ||
watchdog | ||
wheel==0.23.0 |
Oops, something went wrong.