Using C library with PureBasic (LibHTTP)

Just starting out? Need help? Post your questions and find answers here.
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Using C library with PureBasic (LibHTTP)

Post by wombats »

Hi,

I am trying to convert the LibHTTP library to use in PureBasic. I've never done anything like this before, so I am kind of lost.

For the httplib_start command, how can I make use of the callbacks in PB, and what do I pass for options? In the examples, it does it like this:

(From: /libhttp-master/examples/ws_server/ws_server.c)

Code: Select all

    struct httplib_context *ctx;
    struct httplib_callbacks callbacks;
    const char *options[] = {
        "listening_ports", "8080",
        "document_root", "docroot",
        NULL
    };

    // ....

    memset(&callbacks, 0, sizeof(callbacks));
    callbacks.websocket_connect = websocket_connect_handler;
    callbacks.websocket_ready = websocket_ready_handler;
    callbacks.websocket_data = websocket_data_handler;
    callbacks.connection_close = websocket_close_handler;
    ctx = httplib_start(&callbacks, NULL, options);
Would the callbacks be different on each OS? I'm on macOS right now, so I compiled a .a file. Do I need to distribute that with my program or does PB compile it into the executable?

Code: Select all

ImportC "libhttp.a"
  
  ;httplib_cry(*ctx, *conn, *fmt)
  ;httplib_get_context(*conn)
  httplib_get_builtin_mime_type(file_name.p-utf8)
  ;httplib_get_option(
  httplib_get_random()
  ;httplib_get_response_code_text(*conn, response_code.i)
  ;httplib_get_server_ports(*ctx, size.i, *ports)
  ;httplib_get_user_data(*ctx)
  ;httplib_get_valid_options()
  httplib_start(*callbacks, *user_data, *options)
  httplib_stop(*ctx)
  httplib_version()

EndImport
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Using C library with PureBasic (LibHTTP)

Post by infratec »

Maybe something like that:

Code: Select all

; https://www.libhttp.org/httplib_callbacks/

PrototypeC.i Prototype_begin_request(*conn) ; int (*begin_request)( struct httplib_connection * conn )
PrototypeC Prototype_connection_close(*conn) ; void (*connection_close)( const struct httplib_connection *conn )
PrototypeC Prototype_end_request(*conn) ; void (*end_request)( const struct httplib_connection * conn )
PrototypeC Prototype_exit_context(*ctx) ; void (*exit_context)( const struct httplib_context *ctx )
PrototypeC.i Prototype_http_error(*conn, status.i) ; int (*http_error)( struct httplib_connection *conn, int status )
PrototypeC Prototype_init_context(*ctx) ; void (*init_context)( const struct httplib_context *ctx )
PrototypeC Prototype_init_ssl(*ssl_context, *user_data) ; int (*init_ssl)( void *ssl_context, void *user_data )
PrototypeC Prototype_init_thread(*ctx, thread_type.i) ; void (*init_thread)( const struct httplib_context *ctx, int thread_type )
PrototypeC.i Prototype_log_access(*conn, message.p-utf8) ; int (*log_access)( const struct httplib_connection *conn, const char *message )
PrototypeC.i Prototype_log_message(*ctx, *conn, message.p-utf8) ; int (*log_message)( const struct httplib_context *ctx, const struct httplib_connection *conn, const char *message )
PrototypeC.i Prototype_open_file(*conn, path.p-utf8, *data_len) ; const char *(*open_file)( const struct httplib_connection *conn, const char *path, size_t *data_len )


Structure httplib_callbacks_Structure
  begin_request.Prototype_begin_request
  connection_close.Prototype_connection_close
  end_request.Prototype_end_request
  exit_context.Prototype_exit_context
  http_error.Prototype_http_error
  init_context.Prototype_init_context
  init_ssl.Prototype_init_ssl
  init_thread.Prototype_init_thread
  log_access.Prototype_log_access
  log_message.Prototype_log_message
  open_file.Prototype_open_file
EndStructure


ProcedureC InitContext(*ctx)
  Debug "InitContext()"
EndProcedure


ProcedureC HTTPError(*conn, status.i)
  Debug "HTTPError()"
  ProcedureReturn 0
EndProcedure


Define *ctx
Define httplib_callbacks.httplib_callbacks_Structure

httplib_callbacks\init_context = @InitContext()
httplib_callbacks\http_error = @HTTPError()

*ctx = httplib_start(@httplib_callbacks, #Null, #Null)
wombats
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Dec 29, 2011 5:03 pm

Re: Using C library with PureBasic (LibHTTP)

Post by wombats »

Thanks for the reply. That crashes on macOS, unfortunately, when used with a WaitWindowEvent() event loop.
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Using C library with PureBasic (LibHTTP)

Post by infratec »

Maybe you have to enable thread safe, maybe you have to remove all 'C's frome the Prototype and Procedures.

I didn't find a libhttp.dll for windows.
So I can not test it.
Post Reply