Lines Matching refs:parser
134 uri_parse_pct_encoded_data(struct uri_parser *parser,
145 parser->error = "Unexpected URI boundary after '%'";
150 parser->error = p_strdup_printf(parser->pool,
159 parser->error = p_strdup_printf(parser->pool,
167 if (!parser->allow_pct_nul && *ch_r == '\0') {
168 parser->error =
175 int uri_parse_pct_encoded(struct uri_parser *parser,
179 (parser, &parser->cur, parser->end, ch_r);
183 uri_parse_unreserved_char(struct uri_parser *parser, unsigned char *ch_r)
185 if ((*parser->cur & 0x80) != 0)
188 if ((_uri_char_lookup[*parser->cur] & CHAR_MASK_UNRESERVED) != 0) {
189 *ch_r = *parser->cur;
190 parser->cur++;
196 int uri_parse_unreserved(struct uri_parser *parser, string_t *part)
200 while (parser->cur < parser->end) {
204 if ((ret = uri_parse_unreserved_char(parser, &ch)) < 0)
217 int uri_parse_unreserved_pct(struct uri_parser *parser, string_t *part)
221 while (parser->cur < parser->end) {
225 if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0)
228 (ret=uri_parse_unreserved_char(parser, &ch)) < 0)
241 bool uri_data_decode(struct uri_parser *parser, const char *data,
260 decoded = uri_parser_get_tmpbuf(parser, 256);
265 (parser, &p, NULL, &ch)) != 0) {
276 *decoded_r = p_strdup(parser->pool, str_c(decoded));
280 int uri_parse_scheme(struct uri_parser *parser, const char **scheme_r)
282 const unsigned char *first = parser->cur;
289 if (parser->cur >= parser->end || !i_isalpha(*parser->cur))
291 parser->cur++;
294 parser->cur < parser->end) {
295 if (!i_isalnum(*parser->cur) &&
296 *parser->cur != '+' && *parser->cur != '-' &&
297 *parser->cur != '.')
299 parser->cur++;
303 if (parser->cur >= parser->end || *parser->cur != ':') {
304 parser->error = "Invalid URI scheme";
308 *scheme_r = t_strndup(first, parser->cur - first);
309 parser->cur++;
315 struct uri_parser parser;
317 uri_parser_init(&parser, NULL, *uri_p);
318 if (uri_parse_scheme(&parser, scheme_r) <= 0)
320 *uri_p = (const char *)parser.cur;
325 uri_parse_dec_octet(struct uri_parser *parser, string_t *literal,
340 while (parser->cur < parser->end && i_isdigit(*parser->cur)) {
341 octet = octet * 10 + (parser->cur[0] - '0');
346 str_append_c(literal, *parser->cur);
348 parser->cur++;
360 uri_parse_ipv4address(struct uri_parser *parser, string_t *literal,
373 if ((ret = uri_parse_dec_octet(parser, literal, &octet)) <= 0)
377 for (i = 0; i < 3 && parser->cur < parser->end; i++) {
378 if (*parser->cur != '.')
383 parser->cur++;
385 if ((ret = uri_parse_dec_octet(parser, literal, &octet)) <= 0)
396 uri_do_parse_reg_name(struct uri_parser *parser,
404 while (parser->cur < parser->end) {
409 if ((ret=uri_parse_pct_encoded(parser, &c)) < 0)
412 (ret=uri_parse_unreserved_char(parser, &c)) < 0)
422 c = *parser->cur;
425 str_append_c(reg_name, *parser->cur);
426 parser->cur++;
434 int uri_parse_reg_name(struct uri_parser *parser,
441 reg_name = uri_parser_get_tmpbuf(parser, 256);
443 if ((ret=uri_do_parse_reg_name(parser, reg_name)) <= 0)
451 static int uri_do_parse_host_name(struct uri_parser *parser,
485 first = part = parser->cur;
491 offset = parser->cur;
492 ch = pch = *parser->cur;
493 if (parser->cur >= parser->end)
495 if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0) {
502 part = parser->cur;
504 if (!i_isalnum(*parser->cur))
506 parser->cur++;
509 if (parser->cur < parser->end) {
512 offset = parser->cur;
514 if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0) {
524 part = parser->cur;
526 ch = *parser->cur;
529 parser->cur++;
532 } while (parser->cur < parser->end);
535 parser->error = "Invalid domain label in hostname";
540 if (host_name != NULL && parser->cur > part)
541 str_append_n(host_name, part, parser->cur - part);
544 if (parser->cur >= parser->end || ch != '.')
548 if (parser->cur == offset)
549 parser->cur++;
550 part = parser->cur;
553 if (parser->cur == first)
567 int uri_parse_host_name(struct uri_parser *parser,
574 host_name = uri_parser_get_tmpbuf(parser, 256);
576 if ((ret=uri_do_parse_host_name(parser, host_name)) <= 0)
585 uri_parse_ip_literal(struct uri_parser *parser, string_t *literal,
601 for (p = parser->cur+1; p < parser->end; p++) {
606 if (p >= parser->end || *p != ']') {
607 parser->error = "Expecting ']' at end of IP-literal";
612 str_append_n(literal, parser->cur, p-parser->cur+1);
613 address = t_strdup_until(parser->cur+1, p);
614 parser->cur = p + 1;
617 parser->error = "Empty IPv6 host address";
621 parser->error = p_strdup_printf(parser->pool,
626 parser->error = p_strdup_printf(parser->pool,
636 uri_do_parse_host(struct uri_parser *parser,
654 literal = uri_parser_get_tmpbuf(parser, 256);
657 if (parser->cur < parser->end && *parser->cur == '[') {
658 if ((ret=uri_parse_ip_literal(parser, literal, &ip6)) <= 0)
662 host->name = p_strdup(parser->pool, str_c(literal));;
673 preserve = parser->cur;
674 if ((ret = uri_parse_ipv4address(parser, literal, &ip4)) > 0) {
676 host->name = p_strdup(parser->pool, str_c(literal));
682 parser->cur = preserve;
687 if (uri_do_parse_host_name(parser, literal) < 0)
689 } else if (uri_do_parse_reg_name(parser, literal) < 0)
692 host->name = p_strdup(parser->pool, str_c(literal));
696 int uri_parse_host(struct uri_parser *parser,
699 return uri_do_parse_host(parser, host, TRUE);
703 uri_parse_port(struct uri_parser *parser,
714 first = parser->cur;
715 while (parser->cur < parser->end && i_isdigit(*parser->cur))
716 parser->cur++;
718 if (parser->cur == first)
720 if (net_str2port(t_strdup_until(first, parser->cur), &port) < 0) {
721 parser->error = "Invalid port number";
731 uri_do_parse_authority(struct uri_parser *parser,
745 for (p = parser->cur; p < parser->end; p++){
756 if (p < parser->end && *p == '@') {
758 auth->enc_userinfo = p_strdup_until(parser->pool, parser->cur, p);
759 parser->cur = p+1;
763 if (uri_do_parse_host(parser,
766 if (parser->cur == parser->end)
768 switch (*parser->cur) {
772 parser->error = "Invalid host identifier";
777 if (*parser->cur == ':') {
778 parser->cur++;
780 if ((ret = uri_parse_port(parser, auth)) < 0)
782 if (parser->cur == parser->end)
784 switch (*parser->cur) {
788 parser->error = "Invalid host port";
797 uri_do_parse_slashslash_authority(struct uri_parser *parser,
803 if ((parser->end - parser->cur) <= 2 || parser->cur[0] != '/' ||
804 parser->cur[1] != '/')
807 parser->cur += 2;
808 return uri_do_parse_authority(parser, auth, host_name);
811 int uri_parse_authority(struct uri_parser *parser,
814 return uri_do_parse_authority(parser, auth, FALSE);
817 int uri_parse_slashslash_authority(struct uri_parser *parser,
820 return uri_do_parse_slashslash_authority(parser, auth, FALSE);
823 int uri_parse_host_authority(struct uri_parser *parser,
826 return uri_do_parse_authority(parser, auth, TRUE);
829 int uri_parse_slashslash_host_authority(struct uri_parser *parser,
832 return uri_do_parse_slashslash_authority(parser, auth, TRUE);
835 int uri_parse_path_segment(struct uri_parser *parser, const char **segment_r)
837 const unsigned char *first = parser->cur;
840 while (parser->cur < parser->end) {
841 if (*parser->cur == '%') {
843 if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0)
849 if ((*parser->cur & 0x80) != 0 ||
850 (_uri_char_lookup[*parser->cur] & CHAR_MASK_PCHAR) == 0)
853 parser->cur++;
856 if (parser->cur < parser->end &&
857 *parser->cur != '/' && *parser->cur != '?' && *parser->cur != '#' ) {
858 parser->error =
863 if (first == parser->cur)
867 *segment_r = p_strdup_until(parser->pool, first, parser->cur);
871 int uri_parse_path(struct uri_parser *parser,
874 const unsigned char *pbegin = parser->cur;
883 p_array_init(&segments, parser->pool, 16);
890 if (parser->cur < parser->end && *parser->cur == '/') {
891 parser->cur++;
896 if ((ret = uri_parse_path_segment(parser, &segment)) < 0)
934 if (parser->cur >= parser->end || *parser->cur != '/')
936 parser->cur++;
939 if ((ret = uri_parse_path_segment(parser, &segment)) < 0)
948 if (parser->cur == pbegin) {
962 if (parser->cur < parser->end &&
963 *parser->cur != '?' && *parser->cur != '#') {
964 parser->error = "Path component contains invalid character";
970 int uri_parse_query(struct uri_parser *parser, const char **query_r)
972 const unsigned char *first = parser->cur;
981 if (parser->cur >= parser->end || *parser->cur != '?')
983 parser->cur++;
985 while (parser->cur < parser->end) {
986 if (*parser->cur == '%') {
988 if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0)
994 if ((*parser->cur & 0x80) != 0 ||
995 (_uri_char_lookup[*parser->cur] & CHAR_MASK_QCHAR) == 0)
997 parser->cur++;
1000 if (parser->cur < parser->end && *parser->cur != '#') {
1001 parser->error = "Query component contains invalid character";
1006 *query_r = p_strdup_until(parser->pool, first+1, parser->cur);
1010 int uri_parse_fragment(struct uri_parser *parser, const char **fragment_r)
1012 const unsigned char *first = parser->cur;
1022 if (parser->cur >= parser->end || *parser->cur != '#')
1024 parser->cur++;
1026 while (parser->cur < parser->end) {
1027 if (*parser->cur == '%') {
1029 if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0)
1035 if ((*parser->cur & 0x80) != 0 ||
1036 (_uri_char_lookup[*parser->cur] & CHAR_MASK_QCHAR) == 0)
1038 parser->cur++;
1041 if (parser->cur < parser->end) {
1042 parser->error = "Fragment component contains invalid character";
1047 *fragment_r = p_strdup_until(parser->pool, first+1, parser->cur);
1051 void uri_parser_init_data(struct uri_parser *parser,
1054 i_zero(parser);
1055 parser->pool = pool;
1056 parser->begin = parser->cur = data;
1057 parser->end = data + size;
1060 void uri_parser_init(struct uri_parser *parser,
1064 (parser, pool, (const unsigned char *)uri, strlen(uri));
1067 string_t *uri_parser_get_tmpbuf(struct uri_parser *parser, size_t size)
1069 if (parser->tmpbuf == NULL)
1070 parser->tmpbuf = str_new(parser->pool, size);
1072 str_truncate(parser->tmpbuf, 0);
1073 return parser->tmpbuf;
1076 int uri_parse_absolute_generic(struct uri_parser *parser,
1099 (ret=uri_parse_scheme(parser, NULL)) <= 0) {
1101 parser->error = "Missing scheme";
1107 (parser, NULL)) < 0)
1112 ret = uri_parse_path(parser, &relative, NULL);
1114 } else if (parser->cur < parser->end && *parser->cur == '/') {
1115 ret = uri_parse_path(parser, &relative, NULL);
1122 if (uri_parse_query(parser, NULL) < 0)
1126 if ((ret=uri_parse_fragment(parser, NULL)) < 0)
1129 parser->error = "Fragment part not allowed";
1133 i_assert(parser->cur == parser->end);
1163 struct uri_parser parser;
1166 i_zero(&parser);
1167 parser.pool = pool_datastack_create();
1168 parser.begin = parser.cur = data;
1169 parser.end = data + size;
1171 ret = uri_parse_absolute_generic(&parser, flags);
1172 *error_r = parser.error;