• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "private-lib-core.h"
2 
3 #if defined(LWS_WITH_HUBBUB)
4 
5 struct lws_rewrite *
lws_rewrite_create(struct lws * wsi,hubbub_callback_t cb,const char * from,const char * to)6 lws_rewrite_create(struct lws *wsi, hubbub_callback_t cb, const char *from,
7 		   const char *to)
8 {
9 	struct lws_rewrite *r = lws_malloc(sizeof(*r), "rewrite");
10 
11 	if (!r) {
12 		lwsl_err("OOM\n");
13 		return NULL;
14 	}
15 
16 	if (hubbub_parser_create("UTF-8", false, &r->parser) != HUBBUB_OK) {
17 		lws_free(r);
18 
19 		return NULL;
20 	}
21 	r->from = from;
22 	r->from_len = strlen(from);
23 	r->to = to;
24 	r->to_len = strlen(to);
25 	r->params.token_handler.handler = cb;
26 	r->wsi = wsi;
27 	r->params.token_handler.pw = (void *)r;
28 	if (hubbub_parser_setopt(r->parser, HUBBUB_PARSER_TOKEN_HANDLER,
29 				 &r->params) != HUBBUB_OK) {
30 		lws_free(r);
31 
32 		return NULL;
33 	}
34 
35 	return r;
36 }
37 
38 int
lws_rewrite_parse(struct lws_rewrite * r,const unsigned char * in,int in_len)39 lws_rewrite_parse(struct lws_rewrite *r,
40 		  const unsigned char *in, int in_len)
41 {
42 	if (r && hubbub_parser_parse_chunk(r->parser, in, in_len) != HUBBUB_OK)
43 		return -1;
44 
45 	return 0;
46 }
47 
48 void
lws_rewrite_destroy(struct lws_rewrite * r)49 lws_rewrite_destroy(struct lws_rewrite *r)
50 {
51 	hubbub_parser_destroy(r->parser);
52 	lws_free(r);
53 }
54 
55 #endif
56