Skip to content

Commit

Permalink
Support add extra header
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed Apr 24, 2019
1 parent 9fb77a5 commit 79c3590
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion example/example.c
Expand Up @@ -135,7 +135,7 @@ int main(int argc, char **argv)

uwsc_log_info("Libuwsc: %s\n", UWSC_VERSION_STRING);

cl = uwsc_new(loop, url, ping_interval);
cl = uwsc_new(loop, url, ping_interval, NULL);
if (!cl)
return -1;

Expand Down
13 changes: 9 additions & 4 deletions src/uwsc.c
Expand Up @@ -490,7 +490,8 @@ static inline void uwsc_ping(struct uwsc_client *cl)
cl->send(cl, msg, strlen(msg), UWSC_OP_PING);
}

static void uwsc_handshake(struct uwsc_client *cl, const char *host, int port, const char *path)
static void uwsc_handshake(struct uwsc_client *cl, const char *host, int port,
const char *path, const char *extra_header)
{
struct buffer *wb = &cl->wb;
uint8_t nonce[16];
Expand All @@ -510,7 +511,10 @@ static void uwsc_handshake(struct uwsc_client *cl, const char *host, int port, c
buffer_put_string(wb, "\r\n");
else
buffer_put_printf(wb, ":%d\r\n", port);


if (extra_header && *extra_header)
buffer_put_string(wb, extra_header);

buffer_put_string(wb, "\r\n");

ev_io_start(cl->loop, &cl->iow);
Expand Down Expand Up @@ -556,7 +560,8 @@ static void uwsc_timer_cb(struct ev_loop *loop, struct ev_timer *w, int revents)
cl->wait_pong = true;
}

struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url, int ping_interval)
struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url,
int ping_interval, const char *extra_header)
{
struct uwsc_client *cl = NULL;
const char *path = "/";
Expand Down Expand Up @@ -622,7 +627,7 @@ struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url, int ping_int
buffer_set_persistent_size(&cl->rb, UWSC_BUFFER_PERSISTENT_SIZE);
buffer_set_persistent_size(&cl->wb, UWSC_BUFFER_PERSISTENT_SIZE);

uwsc_handshake(cl, host, port, path);
uwsc_handshake(cl, host, port, path, extra_header);

return cl;

Expand Down
10 changes: 6 additions & 4 deletions src/uwsc.h
Expand Up @@ -111,11 +111,13 @@ struct uwsc_client {
};

/*
* uwsc_new - creat an uwsc_client struct and connect to server
* @loop: If NULL will use EV_DEFAULT
* @url: A websock url. ws://xxx.com/xx or wss://xxx.com/xx
* uwsc_new - creat an uwsc_client struct and connect to server
* @loop: If NULL will use EV_DEFAULT
* @url: A websock url. ws://xxx.com/xx or wss://xxx.com/xx
* @ping_interval: ping interval
* @extra_header: extra http header. Authorization: a1d4cdb1a3cd6a0e94aa3599afcddcf5\r\n
*/
struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url, int ping_interval);
struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url,
int ping_interval, const char *extra_header);

#endif

0 comments on commit 79c3590

Please sign in to comment.