Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some bugs in the CoAP-HTTP Proxy #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions coapthon/client/helperclient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import random
import threading
from coapthon.messages.message import Message
from coapthon import defines
from coapthon.client.coap import CoAP
from coapthon.messages.request import Request
Expand Down Expand Up @@ -28,7 +27,7 @@ def __init__(self, server, sock=None, cb_ignore_read_exception=None, cb_ignore_w
:param server: the remote CoAP server
:param sock: if a socket has been created externally, it can be used directly
:param cb_ignore_read_exception: Callback function to handle exception raised during the socket read operation
:param cb_ignore_write_exception: Callback function to handle exception raised during the socket write operation
:param cb_ignore_write_exception: Callback function to handle exception raised during the socket write operation
"""
self.server = server
self.protocol = CoAP(self.server, random.randint(1, 65535), self._wait_response, sock=sock,
Expand Down Expand Up @@ -111,7 +110,7 @@ def cancel_observe_token(self, token, explicit, timeout=None): # pragma: no cov
request.observe = 1

self.send_request(request, callback=None, timeout=timeout)

def cancel_observing(self, response, explicit): # pragma: no cover
"""
Delete observing on the remote server.
Expand All @@ -122,7 +121,7 @@ def cancel_observing(self, response, explicit): # pragma: no cover
"""
self.cancel_observe_token(self, response.token, explicit)

def get(self, path, proxy_uri=None, callback=None, timeout=None, **kwargs): # pragma: no cover
def get(self, path, proxy_uri=None, etag=None, callback=None, timeout=None, **kwargs): # pragma: no cover
"""
Perform a GET on a certain path.

Expand All @@ -136,6 +135,8 @@ def get(self, path, proxy_uri=None, callback=None, timeout=None, **kwargs): # p
request.token = generate_random_token(2)
if proxy_uri:
request.proxy_uri = proxy_uri
if etag:
request.etag = etag

for k, v in kwargs.iteritems():
if hasattr(request, k):
Expand All @@ -162,7 +163,7 @@ def observe(self, path, callback, timeout=None, **kwargs): # pragma: no cover

return self.send_request(request, callback, timeout)

def delete(self, path, proxy_uri=None, callback=None, timeout=None, **kwargs): # pragma: no cover
def delete(self, path, proxy_uri=None, etag=None, callback=None, timeout=None, **kwargs): # pragma: no cover
"""
Perform a DELETE on a certain path.

Expand All @@ -176,14 +177,16 @@ def delete(self, path, proxy_uri=None, callback=None, timeout=None, **kwargs):
request.token = generate_random_token(2)
if proxy_uri:
request.proxy_uri = proxy_uri
if etag:
request.etag = etag

for k, v in kwargs.iteritems():
if hasattr(request, k):
setattr(request, k, v)

return self.send_request(request, callback, timeout)

def post(self, path, payload, proxy_uri=None, callback=None, timeout=None, **kwargs): # pragma: no cover
def post(self, path, payload, proxy_uri=None, etag=None, callback=None, timeout=None, **kwargs): # pragma: no cover
"""
Perform a POST on a certain path.

Expand All @@ -199,14 +202,16 @@ def post(self, path, payload, proxy_uri=None, callback=None, timeout=None, **kwa
request.payload = payload
if proxy_uri:
request.proxy_uri = proxy_uri
if etag:
request.etag = etag

for k, v in kwargs.iteritems():
if hasattr(request, k):
setattr(request, k, v)

return self.send_request(request, callback, timeout)

def put(self, path, payload, proxy_uri=None, callback=None, timeout=None, **kwargs): # pragma: no cover
def put(self, path, payload, proxy_uri=None, etag=None, callback=None, timeout=None, **kwargs): # pragma: no cover
"""
Perform a PUT on a certain path.

Expand All @@ -222,6 +227,8 @@ def put(self, path, payload, proxy_uri=None, callback=None, timeout=None, **kwar
request.payload = payload
if proxy_uri:
request.proxy_uri = proxy_uri
if etag:
request.etag = etag

for k, v in kwargs.iteritems():
if hasattr(request, k):
Expand Down Expand Up @@ -274,7 +281,6 @@ def send_request(self, request, callback=None, timeout=None): # pragma: no cove
del self.requests[request.token]
return context.response


def send_empty(self, empty): # pragma: no cover
"""
Send empty message.
Expand Down
10 changes: 6 additions & 4 deletions coapthon/http_proxy/coap_http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, coap_ip=LOCALHOST, coap_port=COAP_DEFAULT_PORT, path=DEFAULT_
:param coap_ip: the ip of the ch_proxy server
:param coap_port: the port of the ch_proxy server
:param path: the path of the ch_proxy server

"""
global ch_path
ch_path = CHProxy.get_formatted_path(path)
Expand Down Expand Up @@ -97,7 +97,6 @@ def receive_datagram(self, args):
rst.destination = client_address
rst.type = Types["RST"]
rst.code = message
rst.mid = message.mid
self.send_datagram(rst)
return
logger.debug("receive_datagram - " + str(message))
Expand All @@ -110,12 +109,14 @@ def receive_datagram(self, args):
rst.type = Types["RST"]
rst.code = Codes.BAD_REQUEST.number
rst.mid = message.mid
rst.token = message.token
self.send_datagram(rst)
return
# Execute HTTP/HTTPS request
http_response = CoAP_HTTP.execute_http_request(message.code, message.proxy_uri, message.payload)
# HTTP response to CoAP response conversion
coap_response = CoAP_HTTP.to_coap_response(http_response, message.code, client_address, message.mid)
coap_response = CoAP_HTTP.to_coap_response(http_response, message.code, client_address, message.mid,
message.token)
# Send datagram and return
self.send_datagram(coap_response)
return
Expand All @@ -127,12 +128,13 @@ def receive_datagram(self, args):
logger.error("Received response from %s", message.source)

@staticmethod
def to_coap_response(http_response, request_method, client_address, mid):
def to_coap_response(http_response, request_method, client_address, mid, token):
coap_msg = Message()
coap_msg.destination = client_address
coap_msg.type = Types["ACK"]
coap_msg.code = CoAP_HTTP.to_coap_code(http_response.status_code, request_method)
coap_msg.mid = mid
coap_msg.token = token
if 'Content-Type' in http_response.headers:
coap_msg.content_type = CoAP_HTTP.to_coap_content_type(http_response.headers['Content-Type'].split(";")[0])
else:
Expand Down