Project homepage Mailing List  Warmcat.com  API Docs  Github Mirror 
{"schema":"libjg2-1", "vpath":"/git/", "avatar":"/git/avatar/", "alang":"en-US,en;q\u003d0.5", "gen_ut":1575709417, "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":"c76253d17738859858d16684f4b495ca", "oid":{ "oid": "efbab4c602269dbb2a0015db9a00b9feae33ae44", "alias": [ "refs/heads/master"]},"blobname": "minimal-examples/http-client/minimal-http-client/minimal-http-client.c", "blob": "/*\n * lws-minimal-http-client\n *\n * Written in 2010-2019 by Andy Green \u003candy@warmcat.com\u003e\n *\n * This file is made available under the Creative Commons CC0 1.0\n * Universal Public Domain Dedication.\n *\n * This demonstrates the a minimal http client using lws.\n *\n * It visits https://warmcat.com/ and receives the html page there. You\n * can dump the page data by changing the #if 0 below.\n */\n\n#include \u003clibwebsockets.h\u003e\n#include \u003cstring.h\u003e\n#include \u003csignal.h\u003e\n\nstatic int interrupted, bad \u003d 1, status;\n#if defined(LWS_WITH_HTTP2)\nstatic int long_poll;\n#endif\nstatic struct lws *client_wsi;\n\nstatic const lws_retry_bo_t retry \u003d {\n\t.secs_since_valid_ping \u003d 3,\n\t.secs_since_valid_hangup \u003d 10,\n};\n\nstatic int\ncallback_http(struct lws *wsi, enum lws_callback_reasons reason,\n\t void *user, void *in, size_t len)\n{\n\tswitch (reason) {\n\n\t/* because we are protocols[0] ... */\n\tcase LWS_CALLBACK_CLIENT_CONNECTION_ERROR:\n\t\tlwsl_err(\u0022CLIENT_CONNECTION_ERROR: %s\u005cn\u0022,\n\t\t\t in ? (char *)in : \u0022(null)\u0022);\n\t\tinterrupted \u003d 1;\n\t\tbreak;\n\n\tcase LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:\n\t\t{\n\t\t\tchar buf[128];\n\n\t\t\tlws_get_peer_simple(wsi, buf, sizeof(buf));\n\t\t\tstatus \u003d lws_http_client_http_response(wsi);\n\n\t\t\tlwsl_user(\u0022Connected to %s, http response: %d\u005cn\u0022,\n\t\t\t\t\tbuf, status);\n\t\t}\n#if defined(LWS_WITH_HTTP2)\n\t\tif (long_poll) {\n\t\t\tlwsl_user(\u0022%s: Client entering long poll mode\u005cn\u0022, __func__);\n\t\t\tlws_h2_client_stream_long_poll_rxonly(wsi);\n\t\t}\n#endif\n\t\tbreak;\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#if defined(LWS_WITH_HTTP2)\n\t\tif (long_poll) {\n\t\t\tchar dotstar[128];\n\t\t\tlws_strnncpy(dotstar, (const char *)in, len,\n\t\t\t\t sizeof(dotstar));\n\t\t\tlwsl_notice(\u0022long poll rx: %d '%s'\u005cn\u0022, (int)len,\n\t\t\t\t\tdotstar);\n\t\t}\n#endif\n#if 0 /* enable to dump the html */\n\t\t{\n\t\t\tconst char *p \u003d in;\n\n\t\t\twhile (len--)\n\t\t\t\tif (*p \u003c 0x7f)\n\t\t\t\t\tputchar(*p++);\n\t\t\t\telse\n\t\t\t\t\tputchar('.');\n\t\t}\n#endif\n\t\treturn 0; /* don't passthru */\n\n\t/* uninterpreted http content */\n\tcase LWS_CALLBACK_RECEIVE_CLIENT_HTTP:\n\t\t{\n\t\t\tchar buffer[1024 + LWS_PRE];\n\t\t\tchar *px \u003d buffer + LWS_PRE;\n\t\t\tint lenx \u003d sizeof(buffer) - LWS_PRE;\n\n\t\t\tif (lws_http_client_read(wsi, \u0026px, \u0026lenx) \u003c 0)\n\t\t\t\treturn -1;\n\t\t}\n\t\treturn 0; /* don't passthru */\n\n\tcase LWS_CALLBACK_COMPLETED_CLIENT_HTTP:\n\t\tlwsl_user(\u0022LWS_CALLBACK_COMPLETED_CLIENT_HTTP\u005cn\u0022);\n\t\tinterrupted \u003d 1;\n\t\tbad \u003d status !\u003d 200;\n\t\tlws_cancel_service(lws_get_context(wsi)); /* abort poll wait */\n\t\tbreak;\n\n\tcase LWS_CALLBACK_CLOSED_CLIENT_HTTP:\n\t\tinterrupted \u003d 1;\n\t\tbad \u003d status !\u003d 200;\n\t\tlws_cancel_service(lws_get_context(wsi)); /* abort poll wait */\n\t\tbreak;\n\n\tdefault:\n\t\tbreak;\n\t}\n\n\treturn lws_callback_http_dummy(wsi, reason, user, in, len);\n}\n\nstatic const struct lws_protocols protocols[] \u003d {\n\t{\n\t\t\u0022http\u0022,\n\t\tcallback_http,\n\t\t0,\n\t\t0,\n\t},\n\t{ NULL, NULL, 0, 0 }\n};\n\nstatic void\nsigint_handler(int sig)\n{\n\tinterrupted \u003d 1;\n}\n\nstruct args {\n\tint argc;\n\tconst char **argv;\n};\n\nstatic int\nsystem_notify_cb(lws_state_manager_t *mgr, lws_state_notify_link_t *link,\n\t\t int current, int target)\n{\n\tstruct lws_context *context \u003d mgr-\u003eparent;\n\tstruct lws_client_connect_info i;\n\tstruct args *a \u003d lws_context_user(context);\n\tconst char *p;\n\n\tif (current !\u003d LWS_SYSTATE_OPERATIONAL || target !\u003d LWS_SYSTATE_OPERATIONAL)\n\t\treturn 0;\n\n\tlwsl_info(\u0022%s: operational\u005cn\u0022, __func__);\n\n\tmemset(\u0026i, 0, sizeof i); /* otherwise uninitialized garbage */\n\ti.context \u003d context;\n\tif (!lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-n\u0022)) {\n\t\ti.ssl_connection \u003d LCCSCF_USE_SSL;\n#if defined(LWS_WITH_HTTP2)\n\t\t/* requires h2 */\n\t\tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022--long-poll\u0022)) {\n\t\t\tlwsl_user(\u0022%s: long poll mode\u005cn\u0022, __func__);\n\t\t\tlong_poll \u003d 1;\n\t\t}\n#endif\n\t}\n\n\tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-l\u0022)) {\n\t\ti.port \u003d 7681;\n\t\ti.address \u003d \u0022localhost\u0022;\n\t\ti.ssl_connection |\u003d LCCSCF_ALLOW_SELFSIGNED;\n\t} else {\n\t\ti.port \u003d 443;\n\t\ti.address \u003d \u0022warmcat.com\u0022;\n\t}\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 (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-j\u0022))\n\t\ti.ssl_connection |\u003d LCCSCF_ALLOW_SELFSIGNED;\n\n\tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-k\u0022))\n\t\ti.ssl_connection |\u003d LCCSCF_ALLOW_INSECURE;\n\n\tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-m\u0022))\n\t\ti.ssl_connection |\u003d LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;\n\n\tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-e\u0022))\n\t\ti.ssl_connection |\u003d LCCSCF_ALLOW_EXPIRED;\n\n\t/* the default validity check is 5m / 5m10s... -v \u003d 3s / 10s */\n\n\tif (lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022-v\u0022))\n\t\ti.retry_and_idle_policy \u003d \u0026retry;\n\n\tif ((p \u003d lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022--server\u0022)))\n\t\ti.address \u003d p;\n\n\tif ((p \u003d lws_cmdline_option(a-\u003eargc, a-\u003eargv, \u0022--path\u0022)))\n\t\ti.path \u003d p;\n\telse\n\t\ti.path \u003d \u0022/\u0022;\n\n\ti.host \u003d i.address;\n\ti.origin \u003d i.address;\n\ti.method \u003d \u0022GET\u0022;\n\n\ti.protocol \u003d protocols[0].name;\n\ti.pwsi \u003d \u0026client_wsi;\n\n\treturn !lws_client_connect_via_info(\u0026i);\n}\n\nint main(int argc, const char **argv)\n{\n\tlws_state_notify_link_t notifier \u003d { {}, system_notify_cb, \u0022app\u0022 };\n\tlws_state_notify_link_t *na[] \u003d { \u0026notifier, NULL };\n\tstruct lws_context_creation_info info;\n\tstruct lws_context *context;\n\tstruct args args;\n\tint n \u003d 0;\n\t// uint8_t memcert[4096];\n\n\targs.argc \u003d argc;\n\targs.argv \u003d argv;\n\n\tsignal(SIGINT, sigint_handler);\n\n\tmemset(\u0026info, 0, sizeof info); /* otherwise uninitialized garbage */\n\tlws_cmdline_option_handle_builtin(argc, argv, \u0026info);\n\n\tlwsl_user(\u0022LWS minimal http client [-d\u003cverbosity\u003e] [-l] [--h1]\u005cn\u0022);\n\n\tinfo.options \u003d LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;\n\tinfo.port \u003d CONTEXT_PORT_NO_LISTEN; /* we do not run any server */\n\tinfo.protocols \u003d protocols;\n\tinfo.user \u003d \u0026args;\n\tinfo.register_notifier_list \u003d na;\n\n\t/*\n\t * since we know this lws context is only ever going to be used with\n\t * one client wsis / fds / sockets at a time, let lws know it doesn't\n\t * have to use the default allocations for fd tables up to ulimit -n.\n\t * It will just allocate for 1 internal and 1 (+ 1 http2 nwsi) that we\n\t * will use.\n\t */\n\tinfo.fd_limit_per_thread \u003d 1 + 1 + 1;\n\n#if defined(LWS_WITH_MBEDTLS)\n\t/*\n\t * OpenSSL uses the system trust store. mbedTLS has to be told which\n\t * CA to trust explicitly.\n\t */\n\tinfo.client_ssl_ca_filepath \u003d \u0022./warmcat.com.cer\u0022;\n#endif\n#if 0\n\tn \u003d open(\u0022./warmcat.com.cer\u0022, O_RDONLY);\n\tif (n \u003e\u003d 0) {\n\t\tinfo.client_ssl_ca_mem_len \u003d read(n, memcert, sizeof(memcert));\n\t\tinfo.client_ssl_ca_mem \u003d memcert;\n\t\tclose(n);\n\t\tn \u003d 0;\n\t\tmemcert[info.client_ssl_ca_mem_len++] \u003d '\u005c0';\n\t}\n#endif\n\tcontext \u003d lws_create_context(\u0026info);\n\tif (!context) {\n\t\tlwsl_err(\u0022lws init failed\u005cn\u0022);\n\t\treturn 1;\n\t}\n\n\twhile (n \u003e\u003d 0 \u0026\u0026 !interrupted)\n\t\tn \u003d lws_service(context, 0);\n\n\tlws_context_destroy(context);\n\tlwsl_user(\u0022Completed: %s\u005cn\u0022, bad ? \u0022failed\u0022 : \u0022OK\u0022);\n\n\treturn bad;\n}\n","s":{"c":1575385221,"u": 450}} ],"g": 759,"chitpc": 0,"ehitpc": 0, "indexed":0 }