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":1635260226, "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":"d73fdcf8a9ebcf76002e2fede7f0fa1a", "oid":{ "oid": "b5aad92ff0cc58dd1e0abb8ed7a08399b8f47e4b", "alias": [ "refs/heads/main"]},"blobname": "plugins/protocol_fulltext_demo.c", "blob": "/*\n * ws protocol handler plugin for \u0022fulltext demo\u0022\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 * The person who associated a work with this deed has dedicated\n * the work to the public domain by waiving all of his or her rights\n * to the work worldwide under copyright law, including all related\n * and neighboring rights, to the extent allowed by law. You can copy,\n * modify, distribute and perform the work, even for commercial purposes,\n * all without asking permission.\n *\n * These test plugins are intended to be adapted for use in your code, which\n * may be proprietary. So unlike the library itself, they are licensed\n * Public Domain.\n */\n\n#if !defined (LWS_PLUGIN_STATIC)\n#if !defined(LWS_DLL)\n#define LWS_DLL\n#endif\n#if !defined(LWS_INTERNAL)\n#define LWS_INTERNAL\n#endif\n#include \u003clibwebsockets.h\u003e\n#endif\n\n#include \u003cstdlib.h\u003e\n#include \u003cstring.h\u003e\n#include \u003cfcntl.h\u003e\n#include \u003csys/types.h\u003e\n#include \u003csys/stat.h\u003e\n#ifdef WIN32\n#include \u003cio.h\u003e\n#endif\n#include \u003cstdio.h\u003e\n\nstruct vhd_fts_demo {\n\tconst char *indexpath;\n};\n\nstruct pss_fts_demo {\n\tstruct lwsac *result;\n\tstruct lws_fts_result_autocomplete *ac;\n\tstruct lws_fts_result_filepath *fp;\n\n\tuint32_t *li;\n\tint done;\n\n\tuint8_t first:1;\n\tuint8_t ac_done:1;\n\n\tuint8_t fp_init_done:1;\n};\n\nstatic int\ncallback_fts(struct lws *wsi, enum lws_callback_reasons reason, void *user,\n\t void *in, size_t len)\n{\n\tstruct vhd_fts_demo *vhd \u003d (struct vhd_fts_demo *)\n\t\tlws_protocol_vh_priv_get(lws_get_vhost(wsi),\n\t\t\t\t\t lws_get_protocol(wsi));\n\tstruct pss_fts_demo *pss \u003d (struct pss_fts_demo *)user;\n\tuint8_t buf[LWS_PRE + 2048], *start \u003d \u0026buf[LWS_PRE], *p \u003d start,\n\t\t*end \u003d \u0026buf[sizeof(buf) - LWS_PRE - 1];\n\tstruct lws_fts_search_params params;\n\tconst char *ccp \u003d (const char *)in;\n\tstruct lws_fts_result *result;\n\tstruct lws_fts_file *jtf;\n\tint n;\n\n\tswitch (reason) {\n\n\tcase LWS_CALLBACK_PROTOCOL_INIT:\n\t\tvhd \u003d lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),\n\t\t\t lws_get_protocol(wsi),sizeof(struct vhd_fts_demo));\n\t\tif (!vhd)\n\t\t\treturn 0;\n\t\tif (lws_pvo_get_str(in, \u0022indexpath\u0022,\n\t\t\t\t (const char **)\u0026vhd-\u003eindexpath))\n\t\t\treturn 1;\n\n\t\treturn 0;\n\n\tcase LWS_CALLBACK_HTTP:\n\n\t\tpss-\u003efirst \u003d 1;\n\t\tpss-\u003eac_done \u003d 0;\n\n\t\t/*\n\t\t * we have a \u0022subdirectory\u0022 selecting the task\n\t\t *\n\t\t * /a/ \u003d autocomplete\n\t\t * /r/ \u003d results\n\t\t */\n\n\t\tif (strncmp(ccp, \u0022/a/\u0022, 3) \u0026\u0026 strncmp(ccp, \u0022/r/\u0022, 3))\n\t\t\tgoto reply_404;\n\n\t\tmemset(\u0026params, 0, sizeof(params));\n\n\t\tparams.needle \u003d ccp + 3;\n\t\tif (*(ccp + 1) \u003d\u003d 'a')\n\t\t\tparams.flags \u003d LWSFTS_F_QUERY_AUTOCOMPLETE;\n\t\tif (*(ccp + 1) \u003d\u003d 'r')\n\t\t\tparams.flags \u003d LWSFTS_F_QUERY_FILES |\n\t\t\t\t LWSFTS_F_QUERY_FILE_LINES |\n\t\t\t\t LWSFTS_F_QUERY_QUOTE_LINE;\n\t\tparams.max_autocomplete \u003d 10;\n\t\tparams.max_files \u003d 10;\n\n\t\tjtf \u003d lws_fts_open(vhd-\u003eindexpath);\n\t\tif (!jtf) {\n\t\t\tlwsl_err(\u0022unable to open %s\u005cn\u0022, vhd-\u003eindexpath);\n\t\t\t/* we'll inform the client in the JSON */\n\t\t\tgoto reply_200;\n\t\t}\n\n\t\tresult \u003d lws_fts_search(jtf, \u0026params);\n\t\tlws_fts_close(jtf);\n\t\tif (result) {\n\t\t\tpss-\u003eresult \u003d params.results_head;\n\t\t\tpss-\u003eac \u003d result-\u003eautocomplete_head;\n\t\t\tpss-\u003efp \u003d result-\u003efilepath_head;\n\t\t}\n\t\t/* NULL result will be told in the json as \u0022indexed\u0022: 0 */\n\nreply_200:\n\t\tif (lws_add_http_common_headers(wsi, HTTP_STATUS_OK,\n\t\t\t\t\t\t\u0022text/html\u0022,\n\t\t\t\t\tLWS_ILLEGAL_HTTP_CONTENT_LEN, \u0026p, end))\n\t\t\treturn 1;\n\n\t\tif (lws_finalize_write_http_header(wsi, start, \u0026p, end))\n\t\t\treturn 1;\n\n\t\tlws_callback_on_writable(wsi);\n\t\treturn 0;\n\nreply_404:\n\t\tif (lws_add_http_common_headers(wsi, HTTP_STATUS_NOT_FOUND,\n\t\t\t\t\t\t\u0022text/html\u0022,\n\t\t\t\t\tLWS_ILLEGAL_HTTP_CONTENT_LEN, \u0026p, end))\n\t\t\treturn 1;\n\n\t\tif (lws_finalize_write_http_header(wsi, start, \u0026p, end))\n\t\t\treturn 1;\n\t\treturn lws_http_transaction_completed(wsi);\n\n\tcase LWS_CALLBACK_CLOSED_HTTP:\n\t\tif (pss \u0026\u0026 pss-\u003eresult)\n\t\t\tlwsac_free(\u0026pss-\u003eresult);\n\t\tbreak;\n\n\tcase LWS_CALLBACK_HTTP_WRITEABLE:\n\n\t\tif (!pss)\n\t\t\tbreak;\n\n\t\tn \u003d LWS_WRITE_HTTP;\n\t\tif (pss-\u003efirst)\n\t\t\tp +\u003d lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),\n\t\t\t\t\u0022{\u005c\u0022indexed\u005c\u0022: %d, \u005c\u0022ac\u005c\u0022: [\u0022, !!pss-\u003eresult);\n\n\t\twhile (pss-\u003eac \u0026\u0026 lws_ptr_diff(end, p) \u003e 256) {\n\t\t\tp +\u003d lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),\n\t\t\t\t\u0022%c{\u005c\u0022ac\u005c\u0022: \u005c\u0022%s\u005c\u0022,\u005c\u0022matches\u005c\u0022: %d,\u0022\n\t\t\t\t\u0022\u005c\u0022agg\u005c\u0022: %d, \u005c\u0022elided\u005c\u0022: %d}\u0022,\n\t\t\t\tpss-\u003efirst ? ' ' : ',', (char *)(pss-\u003eac + 1),\n\t\t\t\tpss-\u003eac-\u003einstances, pss-\u003eac-\u003eagg_instances,\n\t\t\t\tpss-\u003eac-\u003eelided);\n\n\t\t\tpss-\u003efirst \u003d 0;\n\t\t\tpss-\u003eac \u003d pss-\u003eac-\u003enext;\n\t\t}\n\n\t\tif (!pss-\u003eac_done \u0026\u0026 !pss-\u003eac \u0026\u0026 pss-\u003efp) {\n\t\t\tpss-\u003eac_done \u003d 1;\n\n\t\t\tp +\u003d lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),\n\t\t\t\t\t \u0022], \u005c\u0022fp\u005c\u0022: [\u0022);\n\t\t}\n\n\t\twhile (pss-\u003efp \u0026\u0026 lws_ptr_diff_size_t(end, p) \u003e 256) {\n\t\t\tif (!pss-\u003efp_init_done) {\n\t\t\t\tp +\u003d lws_snprintf((char *)p,\n\t\t\t\t\t\tlws_ptr_diff_size_t(end, p),\n\t\t\t\t\t\u0022%c{\u005c\u0022path\u005c\u0022: \u005c\u0022%s\u005c\u0022,\u005c\u0022matches\u005c\u0022: %d,\u0022\n\t\t\t\t\t\u0022\u005c\u0022origlines\u005c\u0022: %d,\u0022\n\t\t\t\t\t\u0022\u005c\u0022hits\u005c\u0022: [\u0022, pss-\u003efirst ? ' ' : ',',\n\t\t\t\t\t((char *)(pss-\u003efp + 1)) +\n\t\t\t\t\t\tpss-\u003efp-\u003ematches_length,\n\t\t\t\t\tpss-\u003efp-\u003ematches,\n\t\t\t\t\tpss-\u003efp-\u003elines_in_file);\n\n\t\t\t\tpss-\u003eli \u003d ((uint32_t *)(pss-\u003efp + 1));\n\t\t\t\tpss-\u003edone \u003d 0;\n\t\t\t\tpss-\u003efp_init_done \u003d 1;\n\t\t\t\tpss-\u003efirst \u003d 0;\n\t\t\t} else {\n\t\t\t\twhile (pss-\u003edone \u003c pss-\u003efp-\u003ematches \u0026\u0026\n\t\t\t\t lws_ptr_diff(end, p) \u003e 256) {\n\n\t\t\t\t\tp +\u003d lws_snprintf((char *)p,\n\t\t\t\t\t\t\tlws_ptr_diff_size_t(end, p),\n\t\t\t\t\t\t\u0022%c\u005cn{\u005c\u0022l\u005c\u0022:%d,\u005c\u0022o\u005c\u0022:%d,\u0022\n\t\t\t\t\t\t\u0022\u005c\u0022s\u005c\u0022:\u005c\u0022%s\u005c\u0022}\u0022,\n\t\t\t\t\t\t!pss-\u003edone ? ' ' : ',',\n\t\t\t\t\t\tpss-\u003eli[0], pss-\u003eli[1],\n\t\t\t\t\t\t*((const char **)\u0026pss-\u003eli[2]));\n\t\t\t\t\tpss-\u003eli +\u003d 2 + (sizeof(const char *) /\n\t\t\t\t\t\t\tsizeof(uint32_t));\n\t\t\t\t\tpss-\u003edone++;\n\t\t\t\t}\n\n\t\t\t\tif (pss-\u003edone \u003d\u003d pss-\u003efp-\u003ematches) {\n\t\t\t\t\t*p++ \u003d ']';\n\t\t\t\t\tpss-\u003efp_init_done \u003d 0;\n\t\t\t\t\tpss-\u003efp \u003d pss-\u003efp-\u003enext;\n\t\t\t\t\tif (!pss-\u003efp)\n\t\t\t\t\t\t*p++ \u003d '}';\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!pss-\u003eac \u0026\u0026 !pss-\u003efp) {\n\t\t\tn \u003d LWS_WRITE_HTTP_FINAL;\n\t\t\tp +\u003d lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),\n\t\t\t\t\t\t\u0022]}\u0022);\n\t\t}\n\n\t\tif (lws_write(wsi, (uint8_t *)start,\n\t\t\t\tlws_ptr_diff_size_t(p, start), (enum lws_write_protocol)n) !\u003d\n\t\t\t\t\t lws_ptr_diff(p, start))\n\t\t\treturn 1;\n\n\t\tif (n \u003d\u003d LWS_WRITE_HTTP_FINAL) {\n\t\t\tif (pss-\u003eresult)\n\t\t\t\tlwsac_free(\u0026pss-\u003eresult);\n\t\t\tif (lws_http_transaction_completed(wsi))\n\t\t\t\treturn -1;\n\t\t} else\n\t\t\tlws_callback_on_writable(wsi);\n\n\t\treturn 0;\n\n\tdefault:\n\t\tbreak;\n\t}\n\n\treturn lws_callback_http_dummy(wsi, reason, user, in, len);\n}\n\n\n#define LWS_PLUGIN_PROTOCOL_FULLTEXT_DEMO \u005c\n\t{ \u005c\n\t\t\u0022lws-test-fts\u0022, \u005c\n\t\tcallback_fts, \u005c\n\t\tsizeof(struct pss_fts_demo), \u005c\n\t\t0, \u005c\n\t\t0, NULL, 0 \u005c\n\t}\n\n#if !defined (LWS_PLUGIN_STATIC)\n\nLWS_VISIBLE const struct lws_protocols fulltext_demo_protocols[] \u003d {\n\tLWS_PLUGIN_PROTOCOL_FULLTEXT_DEMO\n};\n\nLWS_VISIBLE const lws_plugin_protocol_t fulltext_demo \u003d {\n\t.hdr \u003d {\n\t\t\u0022fulltext demo\u0022,\n\t\t\u0022lws_protocol_plugin\u0022,\n\t\tLWS_BUILD_HASH,\n\t\tLWS_PLUGIN_API_MAGIC\n\t},\n\n\t.protocols \u003d fulltext_demo_protocols,\n\t.count_protocols \u003d LWS_ARRAY_SIZE(fulltext_demo_protocols),\n\t.extensions \u003d NULL,\n\t.count_extensions \u003d 0,\n};\n\n#endif\n","s":{"c":1635260226,"u": 469}} ],"g": 2214,"chitpc": 0,"ehitpc": 0,"indexed":0 , "ab": 1, "si": 0, "db":0, "di":0, "sat":0, "lfc": "0000"}