neuray API Programmer's Manual

http.h File Reference

Description

A lightweight HTTP server.

Code Example

http.h

‎/***************************************************************************************************
 * Copyright 2023 NVIDIA Corporation. All rights reserved.
 **************************************************************************************************/

#ifndef MI_NEURAYLIB_HTTP_H
#define MI_NEURAYLIB_HTTP_H

#include <cstdarg>
#include <cstdio>

#include <mi/base/interface_declare.h>
#include <mi/neuraylib/idata.h>

namespace mi {

namespace http { class IConnection; class IWeb_socket; }
namespace neuraylib { class IBuffer; }



namespace http {













class IRequest
{
public:
    virtual const char* get_request_line() const = 0;

    virtual const char* get_command() const = 0;

    virtual const char* get_protocol() const = 0;

    virtual void set_url( const char* url) = 0;

    virtual const char* get_url() const = 0;

    virtual void set_header( const char* key, const char* value) = 0;

    virtual void remove_header( const char* key) = 0;

    virtual const char* get_header( const char* key) const = 0;

    virtual bool get_header(
        Size index, const char** key_pointer, const char** value_pointer) const = 0;

    virtual void set_argument( const char* key, const char* value) = 0;

    virtual void add_argument( const char* key, const char* value) = 0;

    virtual void remove_argument(const char* key) = 0;

    virtual const char* get_argument( const char* key) const = 0;

    virtual bool get_argument(
        Size index, const char** key_pointer, const char** value_pointer) const = 0;

    virtual const char* get_body() const = 0;
};

class IResponse
{
public:
    virtual void set_result_code( Sint32 code, const char* message) = 0;

    virtual Sint32 
               get_result_code() const = 0;

    virtual void set_header( const char* key, const char* value) = 0;

    virtual void remove_header( const char* key) = 0;

    virtual const char* get_header( const char* key) const = 0;

    virtual bool get_header(
        Size index, const char** key_pointer, const char** value_pointer) const = 0;

    virtual bool was_sent() const = 0;
};

class IData_handler : public
    mi::base::Interface_declare<0x723054d8,0xdfa7,0x4475,0xbc,0xb4,0x44,0x23,0x25,0xea,0x52,0x50>
{
public:
    virtual neuraylib::IBuffer* handle( IConnection* connection, neuraylib::IBuffer* buffer) = 0;
};

class IConnection
{
public:
    virtual const char* get_peer_address() = 0;

    virtual const char* get_local_address() = 0;

    virtual bool print( const char* string) = 0;

    bool printf( const char* string, ...)
#ifdef MI_COMPILER_GCC
        __attribute__((format(printf, 2, 3)))
#endif
    {
        va_list args;
        va_start( args, string);
        char buffer[16384];
#ifdef MI_COMPILER_MSC
        vsnprintf_s( &buffer[0], sizeof( buffer), sizeof( buffer)-1, string, args);
#else
        vsnprintf( &buffer[0], sizeof( buffer), string, args);
#endif
        bool result = this->print( &buffer[0]);
        va_end( args);
        return result;
    }

    virtual bool enqueue( neuraylib::IBuffer* buffer) = 0;

    virtual Sint32 
               flush() = 0;

    virtual bool check_error() = 0;

    virtual IRequest* get_request() = 0;

    virtual IResponse* get_response() = 0;

    virtual void add_data_handler( IData_handler* handler) = 0;

    virtual void set_data_attachment( const char* key, IData* value) = 0;

    virtual IData* get_data_attachment( const char* key) = 0;

    template<class T>
    T* get_data_attachment( const char* key)
    {
        IData* ptr = get_data_attachment( key);
        if( !ptr)
            return 0;
        T* ptr_T = static_cast<T*>( ptr->get_interface( typename T::IID()));
        ptr->release();
        return ptr_T;
    }
    virtual void set_attachment( const char* key, const char* value) = 0;

    virtual const char* get_attachment( const char* key) = 0;

    virtual void remove_attachment( const char* key) = 0;

    virtual Sint32 
               read_data( Size size, char* buffer) = 0;

    virtual bool is_secure_connection() = 0;
};

class IWeb_socket_state_handler : public
    mi::base::Interface_declare<0x70ad6206,0x38f0,0x4f2a,0xb7,0x5d,0x8f,0x90,0x64,0x3e,0xd0,0x06>
{
public:
    virtual void handle( IWeb_socket* web_socket) = 0;
};

class IWeb_socket_data_handler : public
    mi::base::Interface_declare<0xbe989e2c,0xf1e6,0x492a,0xb6,0x42,0x1f,0xd7,0x30,0x1f,0xa2,0x9f>
{
public:
    virtual void handle( IWeb_socket* web_socket, neuraylib::IBuffer* buffer,
        bool binary_frame) = 0;
};

class IWeb_socket : public
    mi::base::Interface_declare<0x52fd1beb,0x4c6f,0x4456,0x86,0x9c,0xfd,0xf4,0x12,0x52,0x0a,0xae>
{
public:
    virtual const char* get_peer_address() const = 0;

    virtual const char* get_local_address() const = 0;

    virtual const char* get_url_path() const = 0;

    enum State
    {
        WS_STATE_INIT,
        WS_STATE_CONNECTING,
        WS_STATE_CONNECTED,
        WS_STATE_CLOSING,
        WS_STATE_CLOSED,
        WS_STATE_ERROR,
        WS_STATE_FORCE_32_BIT = 0xffffffffU
    };

    virtual State 
               get_state() const = 0;

    virtual void set_state_handler( IWeb_socket_state_handler* handler) = 0;

    virtual void set_data_handler( IWeb_socket_data_handler* handler) = 0;

    virtual Difference 
               write( neuraylib::IBuffer* buffer, bool binary_frame = false) = 0;

    virtual bool print( const char* string, bool binary_frame = false) = 0;

    bool printf( const char* string, ...)
#ifdef MI_COMPILER_GCC
        __attribute__((format(printf, 2, 3)))
#endif
    {
        va_list args;
        va_start( args, string);
        char buffer[16384];
#ifdef MI_COMPILER_MSC
        vsnprintf_s( &buffer[0], sizeof( buffer), sizeof( buffer)-1, string, args);
#else
        vsnprintf( &buffer[0], sizeof( buffer), string, args);
#endif
        bool result = this->print( &buffer[0]);
        va_end( args);
        return result;
    }

    virtual void close() = 0;

    virtual IConnection* get_http_connection() = 0;

    virtual void set_max_payload(Uint64 bytes) = 0;

    virtual Uint64 
               get_max_payload() = 0;
};

mi_static_assert( sizeof( IWeb_socket::State) == sizeof( Uint32));

class IWeb_socket_handler : public
    mi::base::Interface_declare<0xb784d27c,0x3b80,0x432e,0x89,0xa0,0x13,0xe7,0x33,0x04,0x82,0x5c>
{
public:
    virtual bool handle( IWeb_socket* web_socket) = 0;
};

class IRequest_handler : public
    mi::base::Interface_declare<0x8747d0dd,0x1e27,0x4413,0xa0,0xd4,0x07,0x60,0x8f,0xed,0xfc,0xf9>
{
public:
    virtual bool handle( IConnection* connection) = 0;
};

class ICGIRequest_handler : public
    mi::base::Interface_declare<0xa7fe482e,0x65f8,0x4a4c,0x87,0x21,0xff,0x19,0x21,0x36,0xda,0xe0>
{
public:
    virtual bool handle( IConnection* connection) = 0;
};

class IResponse_handler : public
    mi::base::Interface_declare<0xa9386278,0x6938,0x45a7,0xa2,0x3b,0xbb,0x35,0xf7,0xe9,0x28,0x20>
{
public:
    virtual void handle( IConnection* connection) = 0;
};

class IServer : public
    mi::base::Interface_declare<0x9923b273,0x082f,0x403a,0x83,0x57,0xcd,0x23,0x9b,0xcf,0x68,0x4c>
{
public:
    virtual Sint32 
               start( const char* listen_address) = 0;

    virtual Sint32 
               start_ssl(
        const char* listen_address, const char* cert_file,
        const char* private_key_file, const char* password) = 0;

    virtual void shutdown() = 0;

    virtual Sint32 
               set_default_mime_type( const char* mime_type) = 0;

    virtual void add_mime_type( const char* extension, const char* mime_type) = 0;

    virtual const char* lookup_mime_type( const char* extension) = 0;

    virtual void install( IRequest_handler* handler) = 0;

    virtual void remove( IRequest_handler* handler) = 0;

    virtual void install( IWeb_socket_handler* handler) = 0;

    virtual void remove( IWeb_socket_handler* handler) = 0;

    virtual void install( ICGIRequest_handler* handler) = 0;

    virtual void remove( ICGIRequest_handler* handler) = 0;

    virtual void install( IResponse_handler* handler) = 0;

    virtual void remove( IResponse_handler* handler) = 0;

    virtual void set_identification( const char* id_string) = 0;

    virtual const char* get_listen_address() = 0;

    virtual const char* get_ssl_listen_address() = 0;

    virtual Size 
               get_nr_of_connections() = 0;

    virtual Size 
               get_nr_of_active_connections() = 0;

    virtual void set_keep_alive_timeout( Uint32 nr_of_seconds) = 0;

    virtual Uint32 
               get_keep_alive_timeout() const = 0;

    virtual void set_send_buffer_size( Uint32 send_buffer_size) = 0;

    virtual Uint32 
               get_send_buffer_size() const = 0;

    virtual void set_http_post_body_limit( Uint32 http_post_body_limit) = 0;

    virtual Uint32 
               get_http_post_body_limit() const = 0;

    //           try again.
    virtual void set_concurrent_connection_limit( Uint32 limit ) = 0;

    virtual Uint32 
               get_concurrent_connection_limit() = 0;
};

class IFactory : public
    mi::base::Interface_declare<0xddded154,0x4be8,0x42b6,0x81,0x68,0x21,0x16,0xc7,0xbd,0x63,0x40>
{
public:
    virtual IServer* create_server() = 0;

    virtual IRequest_handler* create_file_handler(
        const char* root_url, const char* root_path, bool is_recursive = true) = 0;

    virtual IRequest_handler* create_redirect_handler(
        const char* source_url, const char* target_url) = 0;

    virtual IResponse_handler* create_log_handler( const char* path) = 0;

    virtual IResponse_handler* create_chunked_encoding_handler() = 0;

    virtual IWeb_socket* create_client_web_socket( const char* web_socket_address,
        Float32 connect_timeout) = 0;
};
 // end group mi_neuray_http

} // namespace http

} // namespace mi

#endif // MI_NEURAYLIB_HTTP_H

Namespaces

namespace 
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH. More...
namespace 
Namespace for the HTTP server of the neuray API. More...
namespace 
Namespace for the neuray API. More...

Classes

class 
CGI request handlers are responsible for handling HTTP requests. More...
class 
The connection class represents a connection from a client to the server. More...
class 
A data handler may be added to a connection. More...
class 
The factory can be used to instantiate the built-in HTTP classes. More...
class 
This interface holds all the parameters of a request. More...
class 
Request handlers are responsible for handling HTTP requests. More...
class 
This interface holds all the parameters of a response. More...
class 
Response handlers can be used to modify responses generated by request handlers. More...
class 
The server builds a framework for the handlers. More...
class 
The WebSocket connection class represents a connection that is built on top of an HTTP connection. More...
class 
A WebSocket data handler that can be installed to a WebSocket connection to handle data arriving at the WebSocket. More...
class 
WebSocket handlers are responsible for handling WebSocket requests. More...
class 
A WebSocket state handler that can be installed to a WebSocket connection to handle events of the WebSocket. More...