Project homepage Mailing List  Warmcat.com  API Docs  Github Mirror 
{"schema":"libjg2-1", "vpath":"/git/", "avatar":"/git/avatar/", "alang":"", "gen_ut":1752656207, "reponame":"libwebsockets", "desc":"libwebsockets lightweight C networking library", "owner": { "name": "Andy Green", "email": "andy@warmcat.com", "md5": "c50933ca2aa61e0fe2c43d46bb6b59cb" },"url":"https://libwebsockets.org/repo/libwebsockets", "f":3, "items": [ {"schema":"libjg2-1", "cid":"f497c0204d82c34efbeb6dd5596e1510", "commit": {"type":"commit", "time": 1577038665, "time_ofs": 0, "oid_tree": { "oid": "e2a1ed79629988fac784ffe3383a38f85b3bbf6b", "alias": []}, "oid":{ "oid": "36de0ada7d931c21cff8264b61a07ca2baf36aa4", "alias": []}, "msg": "http client basic auth add helper and example", "sig_commit": { "git_time": { "time": 1577038665, "offset": 0 }, "name": "Andy Green", "email": "andy@warmcat.com", "md5": "c50933ca2aa61e0fe2c43d46bb6b59cb" }, "sig_author": { "git_time": { "time": 1576990724, "offset": 0 }, "name": "Andy Green", "email": "andy@warmcat.com", "md5": "c50933ca2aa61e0fe2c43d46bb6b59cb" }}, "body": "http client basic auth add helper and example" , "diff": "diff --git a/include/libwebsockets/lws-client.h b/include/libwebsockets/lws-client.h\nindex e7f602d..e27bbfa 100644\n--- a/include/libwebsockets/lws-client.h\n+++ b/include/libwebsockets/lws-client.h\n@@ -292,4 +292,19 @@ lws_client_http_multipart(struct lws *wsi, const char *name,\n \t\t\t const char *filename, const char *content_type,\n \t\t\t char **p, char *end);\n \n+/**\n+ * lws_http_basic_auth_gen() - helper to encode client basic auth string\n+ *\n+ * \u005cparam user: user name\n+ * \u005cparam pw: password\n+ * \u005cparam buf: where to store base64 result\n+ * \u005cparam len: max usable size of buf\n+ *\n+ * Encodes a username and password in Basic Auth format for use with the\n+ * Authorization header. On return, buf is filled with something like\n+ * \u0022Basic QWxhZGRpbjpPcGVuU2VzYW1l\u0022.\n+ */\n+LWS_VISIBLE LWS_EXTERN int\n+lws_http_basic_auth_gen(const char *user, const char *pw, char *buf, size_t len);\n+\n ///@}\ndiff --git a/lib/roles/http/client/client-http.c b/lib/roles/http/client/client-http.c\nindex 15ea171..a1b8176 100644\n--- a/lib/roles/http/client/client-http.c\n+++ b/lib/roles/http/client/client-http.c\n@@ -1290,6 +1290,27 @@ lws_generate_client_handshake(struct lws *wsi, char *pkt)\n \n #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)\n \n+int\n+lws_http_basic_auth_gen(const char *user, const char *pw, char *buf, size_t len)\n+{\n+\tsize_t n \u003d strlen(user), m \u003d strlen(pw);\n+\tchar b[128];\n+\n+\tif (len \u003c 6 + ((4 * (n + m + 1)) / 3) + 1)\n+\t\treturn 1;\n+\n+\tmemcpy(buf, \u0022Basic \u0022, 6);\n+\n+\tn \u003d lws_snprintf(b, sizeof(b), \u0022%s:%s\u0022, user, pw);\n+\tif (n \u003e\u003d sizeof(b) - 2)\n+\t\treturn 2;\n+\n+\tlws_b64_encode_string(b, n, buf + 6, len - 6);\n+\tbuf[len - 1] \u003d '\u005c0';\n+\n+\treturn 0;\n+}\n+\n LWS_VISIBLE int\n lws_http_client_read(struct lws *wsi, char **buf, int *len)\n {\ndiff --git a/minimal-examples/http-client/minimal-http-client/README.md b/minimal-examples/http-client/minimal-http-client/README.md\nindex 9baedce..9387f8c 100644\n--- a/minimal-examples/http-client/minimal-http-client/README.md\n+++ b/minimal-examples/http-client/minimal-http-client/README.md\n@@ -22,6 +22,9 @@ Commandline option|Meaning\n -m|Apply tls option LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK\n -e|Apply tls option LCCSCF_ALLOW_EXPIRED\n -v|Connection validity use 3s / 10s instead of default 5m / 5m10s\n+--nossl| disable ssl connection\n+--user \u003cusername\u003e| Set Basic Auth username\n+--password \u003cpassword\u003e | Set Basic Auth password\n \n ```\n $ ./lws-minimal-http-client\n@@ -62,4 +65,12 @@ Commandline option|Meaning\n [2018/03/04 14:43:23:3042] USER: Completed\n ```\n \n+You can also test the client Basic Auth support against the http-server/minimal-http-server-basicauth\n+example. In one console window run the server and in the other\n+\n+```\n+$ lws-minimal-http-client -l --nossl --path /secret/index.html --user user --password password\n+```\n+\n+The Basic Auth credentials for the test server are literally username \u0022user\u0022 and password \u0022password\u0022.\n \ndiff --git a/minimal-examples/http-client/minimal-http-client/minimal-http-client.c b/minimal-examples/http-client/minimal-http-client/minimal-http-client.c\nindex d5a6e96..399e302 100644\n--- a/minimal-examples/http-client/minimal-http-client/minimal-http-client.c\n+++ b/minimal-examples/http-client/minimal-http-client/minimal-http-client.c\n@@ -21,6 +21,7 @@ static int interrupted, bad \u003d 1, status;\n static int long_poll;\n #endif\n static struct lws *client_wsi;\n+static const char *ba_user, *ba_password;\n \n static const lws_retry_bo_t retry \u003d {\n \t.secs_since_valid_ping \u003d 3,\n@@ -58,6 +59,24 @@ callback_http(struct lws *wsi, enum lws_callback_reasons reason,\n #endif\n \t\tbreak;\n \n+\t/* you only need this if you need to do Basic Auth */\n+\tcase LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:\n+\t{\n+\t\tunsigned char **p \u003d (unsigned char **)in, *end \u003d (*p) + len;\n+\t\tchar b[128];\n+\n+\t\tif (!ba_user || !ba_password)\n+\t\t\tbreak;\n+\n+\t\tif (lws_http_basic_auth_gen(ba_user, ba_password, b, sizeof(b)))\n+\t\t\tbreak;\n+\t\tif (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_AUTHORIZATION,\n+\t\t\t\t(unsigned char *)b, strlen(b), p, end))\n+\t\t\treturn -1;\n+\n+\t\tbreak;\n+\t}\n+\n \t/* chunks of chunked content, with header removed */\n \tcase LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:\n \t\tlwsl_user(\u0022RECEIVE_CLIENT_HTTP_READ: read %d\u005cn\u0022, (int)len);\n@@ -172,12 +191,21 @@ system_notify_cb(lws_state_manager_t *mgr, lws_state_notify_link_t *link,\n \t\ti.address \u003d \u0022warmcat.com\u0022;\n \t}\n \n+\tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022--nossl\u0022))\n+\t\ti.ssl_connection \u003d 0;\n+\n+\n \tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022--h1\u0022))\n \t\ti.alpn \u003d \u0022http/1.1\u0022;\n \n \tif ((p \u003d lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-p\u0022)))\n \t\ti.port \u003d atoi(p);\n \n+\tif ((p \u003d lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022--user\u0022)))\n+\t\tba_user \u003d p;\n+\tif ((p \u003d lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022--password\u0022)))\n+\t\tba_password \u003d p;\n+\n \tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-j\u0022))\n \t\ti.ssl_connection |\u003d LCCSCF_ALLOW_SELFSIGNED;\n \n","s":{"c":1752656207,"u": 3964}} ],"g": 5100,"chitpc": 0,"ehitpc": 0,"indexed":0 , "ab": 0, "si": 0, "db":0, "di":0, "sat":0, "lfc": "0000"}