Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Added the possibility of passing an external socket to HTTP20Connection #418

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

ocampana
Copy link

ONVIF defines the Uplink service based on HTTP/2 and connection
reversal, in order to have cameras connect to cloud services while
having NAT between themselves and the remote service (For details,
https://www.onvif.org/specs/srv/uplink/ONVIF-Uplink-Spec.pdf)

With this patch, it is possible on the cloud side to accept an incoming
connection from a listining socket and to pass the new socket to
HTTP20Connection, so that the cloud software can use the reverted
connection and turn itself into a client.

Example code for implementing it:

import socket, ssl, time

from hyper import HTTP20Connection
from hyper.common.bufsocket import BufferedSocket

context = ssl.SSLContext (ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain ("server.cert", "server.key")

bindsocket = socket.socket ()
bindsocket.bind(('', 8081))
bindsocket.listen(5)

with context.wrap_socket (bindsocket, server_side=True) as ssock:
    while True:
        newsocket, fromaddr = ssock.accept()

        req = newsocket.read ()

        if b'Connection: Upgrade' in req and b'Upgrade: h2c-reverse':
            newsocket.write (b'HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: h2c-reverse\r\n\r\n')
            newsocket.write (b'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n')

            c = HTTP20Connection ('unused.org', external_socket = BufferedSocket (newsocket))

            c.request('POST', '/onvif/device_service', headers = { 'Content-Type': 'application/soap+xml; charset=utf-8'}, body=b'<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:tt="http://www.onvif.org/ver10/schema"><soap:Body><tds:GetDeviceInformation /></soap:Body></soap:Envelope>'))
            resp = c.get_response ()
            print (resp.read ())

ONVIF  defines  the  Uplink  service  based  on  HTTP/2  and  connection
reversal,  in order  to have  cameras  connect to  cloud services  while
having  NAT between  themselves  and the  remote  service (For  details,
https://www.onvif.org/specs/srv/uplink/ONVIF-Uplink-Spec.pdf)

With this patch, it is possible on  the cloud side to accept an incoming
connection  from a  listining  socket  and to  pass  the  new socket  to
HTTP20Connection,  so  that the  cloud  software  can use  the  reverted
connection and turn itself into a client.

Example code for implementing it:

import socket, ssl, time

from hyper import HTTP20Connection
from hyper.common.bufsocket import BufferedSocket

context = ssl.SSLContext (ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain ("server.cert", "server.key")

bindsocket = socket.socket ()
bindsocket.bind(('', 8081))
bindsocket.listen(5)

with context.wrap_socket (bindsocket, server_side=True) as ssock:
    while True:
        newsocket, fromaddr = ssock.accept()

        req = newsocket.read ()

        if b'Connection: Upgrade' in req and b'Upgrade: h2c-reverse':
            newsocket.write (b'HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: h2c-reverse\r\n\r\n')
            newsocket.write (b'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n')

            c = HTTP20Connection ('unused.org', external_socket = BufferedSocket (newsocket))

            c.request('POST', '/onvif/device_service', headers = { 'Content-Type': 'application/soap+xml; charset=utf-8'}, body=b'<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:tt="http://www.onvif.org/ver10/schema"><soap:Body><tds:GetDeviceInformation /></soap:Body></soap:Envelope>'))
            resp = c.get_response ()
            print (resp.read ())
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant