Commit 83fe5a17 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Make mg_connect_{http,ws} use mg_parse_uri

Remove the specialized URI parser, clean up code a bit.

Fix parsing of URIs with IPv6 hosts (http://[2001:2:3::4]:567/)

PUBLISHED_FROM=968ad97585d928123106ce3828920ee073113f83
parent 49bbfaf1
...@@ -11,6 +11,7 @@ items: ...@@ -11,6 +11,7 @@ items:
- { name: mg_send_websocket_handshake.md } - { name: mg_send_websocket_handshake.md }
- { name: mg_send_websocket_handshake2.md } - { name: mg_send_websocket_handshake2.md }
- { name: mg_send_websocket_handshake3.md } - { name: mg_send_websocket_handshake3.md }
- { name: mg_send_websocket_handshake3v.md }
- { name: mg_set_protocol_http_websocket.md } - { name: mg_set_protocol_http_websocket.md }
- { name: mg_url_decode.md } - { name: mg_url_decode.md }
- { name: struct_http_message.md } - { name: struct_http_message.md }
......
...@@ -9,5 +9,6 @@ signature: | ...@@ -9,5 +9,6 @@ signature: |
Sends multiple websocket frames. Sends multiple websocket frames.
Like `mg_send_websocket_frame()`, but composes a frame from multiple buffers. Like `mg_send_websocket_frame()`, but composes a frame from multiple
*buffers.
---
title: "mg_send_websocket_handshake3v()"
decl_name: "mg_send_websocket_handshake3v"
symbol_kind: "func"
signature: |
void mg_send_websocket_handshake3v(struct mg_connection *nc,
const struct mg_str path,
const struct mg_str host,
const struct mg_str protocol,
const struct mg_str extra_headers,
const struct mg_str user,
const struct mg_str pass);
---
Same as mg_send_websocket_handshake3 but with strings not necessarily
NUL-temrinated
...@@ -13,6 +13,7 @@ Source string is specified by (`src`, `src_len`), and destination is ...@@ -13,6 +13,7 @@ Source string is specified by (`src`, `src_len`), and destination is
(`dst`, `dst_len`). If `is_form_url_encoded` is non-zero, then (`dst`, `dst_len`). If `is_form_url_encoded` is non-zero, then
`+` character is decoded as a blank space character. This function `+` character is decoded as a blank space character. This function
guarantees to NUL-terminate the destination. If destination is too small, guarantees to NUL-terminate the destination. If destination is too small,
then the source string is partially decoded and `-1` is returned. Otherwise, then the source string is partially decoded and `-1` is returned.
*Otherwise,
a length of the decoded string is returned, not counting final NUL. a length of the decoded string is returned, not counting final NUL.
...@@ -3,7 +3,7 @@ title: "mg_parse_uri()" ...@@ -3,7 +3,7 @@ title: "mg_parse_uri()"
decl_name: "mg_parse_uri" decl_name: "mg_parse_uri"
symbol_kind: "func" symbol_kind: "func"
signature: | signature: |
int mg_parse_uri(struct mg_str uri, struct mg_str *scheme, int mg_parse_uri(const struct mg_str uri, struct mg_str *scheme,
struct mg_str *user_info, struct mg_str *host, struct mg_str *user_info, struct mg_str *host,
unsigned int *port, struct mg_str *path, struct mg_str *query, unsigned int *port, struct mg_str *path, struct mg_str *query,
struct mg_str *fragment); struct mg_str *fragment);
......
...@@ -3,7 +3,8 @@ title: "mg_basic_auth_header()" ...@@ -3,7 +3,8 @@ title: "mg_basic_auth_header()"
decl_name: "mg_basic_auth_header" decl_name: "mg_basic_auth_header"
symbol_kind: "func" symbol_kind: "func"
signature: | signature: |
void mg_basic_auth_header(const char *user, const char *pass, struct mbuf *buf); void mg_basic_auth_header(const struct mg_str user, const struct mg_str pass,
struct mbuf *buf);
--- ---
Generate a Basic Auth header and appends it to buf. Generate a Basic Auth header and appends it to buf.
......
This diff is collapsed.
...@@ -1774,6 +1774,8 @@ struct mg_str mg_mk_str_n(const char *s, size_t len); ...@@ -1774,6 +1774,8 @@ struct mg_str mg_mk_str_n(const char *s, size_t len);
/* Macro for initializing mg_str. */ /* Macro for initializing mg_str. */
#define MG_MK_STR(str_literal) \ #define MG_MK_STR(str_literal) \
{ str_literal, sizeof(str_literal) - 1 } { str_literal, sizeof(str_literal) - 1 }
#define MG_NULL_STR \
{ NULL, 0 }
/* /*
* Cross-platform version of `strcmp()` where where first string is * Cross-platform version of `strcmp()` where where first string is
...@@ -3828,7 +3830,7 @@ extern "C" { ...@@ -3828,7 +3830,7 @@ extern "C" {
* *
* Returns 0 on success, -1 on error. * Returns 0 on success, -1 on error.
*/ */
int mg_parse_uri(struct mg_str uri, struct mg_str *scheme, int mg_parse_uri(const struct mg_str uri, struct mg_str *scheme,
struct mg_str *user_info, struct mg_str *host, struct mg_str *user_info, struct mg_str *host,
unsigned int *port, struct mg_str *path, struct mg_str *query, unsigned int *port, struct mg_str *path, struct mg_str *query,
struct mg_str *fragment); struct mg_str *fragment);
...@@ -4056,7 +4058,8 @@ void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len); ...@@ -4056,7 +4058,8 @@ void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len);
* If pass is NULL, then user is expected to contain the credentials pair * If pass is NULL, then user is expected to contain the credentials pair
* already encoded as `user:pass`. * already encoded as `user:pass`.
*/ */
void mg_basic_auth_header(const char *user, const char *pass, struct mbuf *buf); void mg_basic_auth_header(const struct mg_str user, const struct mg_str pass,
struct mbuf *buf);
#ifdef __cplusplus #ifdef __cplusplus
} }
...@@ -4280,6 +4283,17 @@ void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path, ...@@ -4280,6 +4283,17 @@ void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path,
const char *host, const char *protocol, const char *host, const char *protocol,
const char *extra_headers, const char *user, const char *extra_headers, const char *user,
const char *pass); const char *pass);
/* Same as mg_send_websocket_handshake3 but with strings not necessarily
* NUL-temrinated */
void mg_send_websocket_handshake3v(struct mg_connection *nc,
const struct mg_str path,
const struct mg_str host,
const struct mg_str protocol,
const struct mg_str extra_headers,
const struct mg_str user,
const struct mg_str pass);
/* /*
* Helper function that creates an outbound WebSocket connection. * Helper function that creates an outbound WebSocket connection.
* *
...@@ -4341,7 +4355,8 @@ void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags, ...@@ -4341,7 +4355,8 @@ void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags,
/* /*
* Sends multiple websocket frames. * Sends multiple websocket frames.
* *
* Like `mg_send_websocket_frame()`, but composes a frame from multiple buffers. * Like `mg_send_websocket_frame()`, but composes a frame from multiple
*buffers.
*/ */
void mg_send_websocket_framev(struct mg_connection *nc, int op_and_flags, void mg_send_websocket_framev(struct mg_connection *nc, int op_and_flags,
const struct mg_str *strings, int num_strings); const struct mg_str *strings, int num_strings);
...@@ -4386,7 +4401,8 @@ void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags, ...@@ -4386,7 +4401,8 @@ void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags,
* (`dst`, `dst_len`). If `is_form_url_encoded` is non-zero, then * (`dst`, `dst_len`). If `is_form_url_encoded` is non-zero, then
* `+` character is decoded as a blank space character. This function * `+` character is decoded as a blank space character. This function
* guarantees to NUL-terminate the destination. If destination is too small, * guarantees to NUL-terminate the destination. If destination is too small,
* then the source string is partially decoded and `-1` is returned. Otherwise, * then the source string is partially decoded and `-1` is returned.
*Otherwise,
* a length of the decoded string is returned, not counting final NUL. * a length of the decoded string is returned, not counting final NUL.
*/ */
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int mg_url_decode(const char *src, int src_len, char *dst, int dst_len,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment