Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to perform as an HTTP client using libhttp? #74

Open
HsuJv opened this issue Oct 3, 2021 · 1 comment
Open

How to perform as an HTTP client using libhttp? #74

HsuJv opened this issue Oct 3, 2021 · 1 comment

Comments

@HsuJv
Copy link

HsuJv commented Oct 3, 2021

Hi dear @lammertb ,

I'm trying to embed the libhttp in my own projects to act as both the server & client.

But actually, I encountered some challenges when trying to connect to an HTTPS server using libhttp.

  1. At first I only had an instance of struct lh_ctx_t to set up the server, which I then passed to httplib_connect_client. Everything went well, except that it crashed when I tried to call httplib_stop (src/httplib_free_context.c:100). It was trying to clean up SSL_CTX, which was overwritten by another context when it acted as an HTTPS client.
  2. Then I literally realized that I should call httplib_create_client_context to generate another instance of struct lh_ctx_t for each client connection. But this time it crashes again in httplib_close_connection (src/httplib_close_connection.c:124) because there is no place to initialize client_ctx->workerthreadids. Also, client_ctx is always in the CTX_STATUS_TERMINATED status after creation and cannot be CTX_STATUS_RUNNING, which prevents me from sending Hello to other servers.

Could you please give me some advice?

Thanks in advance.

Regards.

@HsuJv
Copy link
Author

HsuJv commented Oct 4, 2021

I have used this change to make a temporary solution, if there is anything else to add please let me know.

diff --git a/3rd_src/libhttp-1.8/src/httplib_close_connection.c b/3rd_src/libhttp-1.8/src/httplib_close_connection.c
index 2e84178..006ba5e 100644
--- a/3rd_src/libhttp-1.8/src/httplib_close_connection.c
+++ b/3rd_src/libhttp-1.8/src/httplib_close_connection.c
@@ -124,8 +124,7 @@ void httplib_close_connection( struct lh_ctx_t *ctx, struct lh_con_t *conn ) {
                        if ( client_ctx->workerthreadids[i] != 0 ) httplib_pthread_join( client_ctx->workerthreadids[i], NULL );
                }
 
-               client_ctx->workerthreadids = httplib_free( client_ctx->workerthreadids );
-               client_ctx                  = httplib_free( client_ctx                  );
+               httplib_destroy_client_context(client_ctx);
 
                httplib_pthread_mutex_destroy( & conn->mutex );
 
diff --git a/3rd_src/libhttp-1.8/src/httplib_connect_client.c b/3rd_src/libhttp-1.8/src/httplib_connect_client.c
index 1464ae9..c46ff96 100644
--- a/3rd_src/libhttp-1.8/src/httplib_connect_client.c
+++ b/3rd_src/libhttp-1.8/src/httplib_connect_client.c
@@ -40,7 +40,8 @@ static struct lh_con_t *      httplib_connect_client_impl( struct lh_ctx_t *ctx, cons
  */
 
 LIBHTTP_API struct lh_con_t *httplib_connect_client_secure( struct lh_ctx_t *ctx, const struct httplib_client_options *client_options ) {
-
+       ctx->status = CTX_STATUS_RUNNING;
+       ctx->num_threads = 0;
        return httplib_connect_client_impl( ctx, client_options, true );
 
 }  /* httplib_connect_client_secure */
@@ -59,7 +60,8 @@ struct lh_con_t *httplib_connect_client( struct lh_ctx_t *ctx, const char *host,
        memset( &opts, 0, sizeof(opts) );
        opts.host = host;
        opts.port = port;
-
+       ctx->status = CTX_STATUS_RUNNING;
+       ctx->num_threads = 0;
        return httplib_connect_client_impl( ctx, &opts, use_ssl );
 
 }  /* httplib_connect_client */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant