Skip to content

Commit

Permalink
1. introducing new anonymous syntax
Browse files Browse the repository at this point in the history
2. TLSServer separation from http module
  • Loading branch information
mcfriend99 committed Mar 20, 2023
1 parent 3b978cd commit 5740d6c
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 85 deletions.
6 changes: 3 additions & 3 deletions libs/http/index.b
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ def delete(url) {
}

/**
* server(port: int, address: string [, secure: bool = false])
* server(port: int, address: string)
*
* Creates an new HttpServer instance.
* @returns HttpServer
* @throws Exception, SocketExcepion, HttpException
*/
def server(port, address, secure) {
return HttpServer(port, address, secure)
def server(port, address) {
return HttpServer(port, address)
}
6 changes: 3 additions & 3 deletions libs/http/request.b
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import curl {
CurlMime,
Auth
}
import ssl
# import ssl

/**
* Http request handler and object.
Expand Down Expand Up @@ -326,8 +326,8 @@ class HttpRequest {

if !is_string(raw_data)
die HttpException('raw_data must be string')
if !instance_of(client, socket.Socket) and !instance_of(client, ssl.TLSSocket)
die HttpException('invalid Socket')
# if !instance_of(client, socket.Socket)
# die HttpException('invalid Socket')

self.ip = client.info().address

Expand Down
76 changes: 3 additions & 73 deletions libs/http/server.b
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ import .status

import socket as so
import iters
import ssl

/**
* HTTP server
* @printable
*/
class HttpServer {

/**
* A boolean value indicating if the server should/will be TLS/SSL secured or not.
* @default false
*/
var is_secure = false

/**
* The host address to which this server will be bound
* @default socket.IP_LOCAL (127.0.0.1)
Expand Down Expand Up @@ -62,27 +55,6 @@ class HttpServer {
*/
var write_timeout = 2000

/**
* The SSL/TLS ceritificate file that will be used be used by a secured server for
* serving requests.
* @note do not set a value to it directly. Use `load_certs()` instead.
*/
var cert_file

/**
* The SSL/TLS private key file that will be used be used by a secured server for
* serving requests.
* @note do not set a value to it directly. Use `load_certs()` instead.
*/
var private_key_file

/**
* This value controls whether the client certificate should be verified
* or not.
* @boolean
*/
var verify_certs = true

# status trackers.
var _is_listening = false

Expand All @@ -96,10 +68,10 @@ class HttpServer {
var _error_listeners = []

/**
* HttpServer(port: int [, host: string [, is_secure: bool]])
* HttpServer(port: int [, host: string])
* @constructor
*/
HttpServer(port, host, is_secure) {
HttpServer(port, host) {

if !is_int(port) or port <= 0
die HttpException('invalid port number')
Expand All @@ -109,39 +81,7 @@ class HttpServer {
die HttpException('invalid host')
else if host != nil self.host = host

if is_secure != nil and !is_bool(is_secure)
die Exception('is_secure must be boolean')
if !is_secure is_secure = false

self.socket = !is_secure ? so.Socket() : ssl.TLSSocket()
# self.socket = so.Socket()
self.is_secure = is_secure
}

/**
* load_certs(cert_file: string | file [, private_key_file: string | file])
*
* loads the given SSL/TLS certificate pairs for the given SSL/TLS context.
* @note certificates can only be loaded for secure servers.
* @return bool
*/
load_certs(cert_file, private_key_file) {
if !self.is_secure
die HttpException('certificates can only be loaded for secure servers')

if !private_key_file private_key_file = cert_file

self.socket.get_context().set_verify(self.verify_certs ? ssl.SSL_VERIFY_PEER : ssl.SSL_VERIFY_NONE)

if self.socket.get_context().load_certs(cert_file, private_key_file) {
self.cert_file = cert_file
self.private_key_file = private_key_file

return self.socket.get_context().set_ciphers(self._ciphers)
} else {
# die Exception('could not load certificate(s)')
return false
}
self.socket = so.Socket()
}

/**
Expand All @@ -153,8 +93,6 @@ class HttpServer {
self._is_listening = false
if !self.socket.is_closed
self.socket.close()
if self.is_secure
self.socket.get_context().free() # close the TLS socket context.
}

/**
Expand Down Expand Up @@ -291,13 +229,6 @@ class HttpServer {
* connection from HTTP clients.
*/
listen() {
if self.is_secure {
if !self.cert_file
die HttpException('no certificate loaded for secure server')
if !self.private_key_file
die HttpException('no private key loaded for secure server')
}

if !self.socket.is_listening {
self.socket.set_option(so.SO_REUSEADDR, is_bool(self.resuse_address) ? self.resuse_address : true)
self.socket.bind(self.port, self.host)
Expand All @@ -321,7 +252,6 @@ class HttpServer {
var data = client.receive()

if data {
if self.is_secure data = to_string(data)
self._process_received(data, client)
}
} catch Exception e {
Expand Down
14 changes: 13 additions & 1 deletion packages/ssl/ssl/index.b
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ import .constants { * }
import .context { * }
import .ssl { * }
import .bio { * }
import .socket { * }
import .socket { * }
import .server { * }

/**
* server(port: int, address: string)
*
* Creates an new TLSServer instance.
* @returns TLSServer
* @throws Exception, SocketExcepion, HttpException
*/
def server(port, address) {
return TLSServer(port, address)
}
Loading

0 comments on commit 5740d6c

Please sign in to comment.