Skip to content

Commit

Permalink
upgraded to v0.0.77
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfriend99 committed Dec 30, 2022
1 parent 31b0139 commit ac0f11f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 71 deletions.
6 changes: 3 additions & 3 deletions libs/http/index.b
Expand Up @@ -140,12 +140,12 @@ def delete(url) {
}

/**
* server(port: int, address: string, is_secure: bool)
* server(port: int, address: string)
*
* Creates an new HttpServer instance.
* @returns HttpServer
* @throws Exception, SocketExcepion, HttpException
*/
def server(port, address, is_secure) {
return HttpServer(port, address, is_secure)
def server(port, address) {
return HttpServer(port, address)
}
5 changes: 2 additions & 3 deletions libs/http/request.b
Expand Up @@ -20,7 +20,6 @@ import curl {
CurlMime,
Auth
}
import ssl

/**
* Http request handler and object.
Expand Down Expand Up @@ -326,8 +325,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 or TLSSocket')
if !instance_of(client, socket.Socket)
die HttpException('invalid Socket')

self.ip = client.info().address

Expand Down
68 changes: 3 additions & 65 deletions libs/http/server.b
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,20 +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.
Expand All @@ -96,10 +75,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 +88,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 +100,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 +236,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 Down

0 comments on commit ac0f11f

Please sign in to comment.