Commit 189cd8c5 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Add mg_assemble_uri

PUBLISHED_FROM=533e134a48e0fca55509c3eb16b0a6b64c1188e8
parent 692f436b
...@@ -3,6 +3,7 @@ title: "URI" ...@@ -3,6 +3,7 @@ title: "URI"
symbol_kind: "intro" symbol_kind: "intro"
decl_name: "uri.h" decl_name: "uri.h"
items: items:
- { name: mg_assemble_uri.md }
- { name: mg_parse_uri.md } - { name: mg_parse_uri.md }
--- ---
......
---
title: "mg_assemble_uri()"
decl_name: "mg_assemble_uri"
symbol_kind: "func"
signature: |
int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info,
const struct mg_str *host, unsigned int port,
const struct mg_str *path, const struct mg_str *query,
const struct mg_str *fragment, int normalize_path,
struct mg_str *uri);
---
Assemble URI from parts. Any of the inputs can be NULL or zero-length mg_str.
If normalize_path is true, path is normalized by resolving relative refs.
Result is a heap-allocated string (uri->p must be free()d after use).
Returns 0 on success, -1 on error.
...@@ -5021,6 +5021,76 @@ int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out) { ...@@ -5021,6 +5021,76 @@ int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out) {
out->len = d - cp; out->len = d - cp;
return 1; return 1;
} }
int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info,
const struct mg_str *host, unsigned int port,
const struct mg_str *path, const struct mg_str *query,
const struct mg_str *fragment, int normalize_path,
struct mg_str *uri) {
int result = -1;
struct mbuf out;
mbuf_init(&out, 0);
if (scheme != NULL && scheme->len > 0) {
mbuf_append(&out, scheme->p, scheme->len);
mbuf_append(&out, "://", 3);
}
if (user_info != NULL && user_info->len > 0) {
mbuf_append(&out, user_info->p, user_info->len);
mbuf_append(&out, "@", 1);
}
if (host != NULL && host->len > 0) {
mbuf_append(&out, host->p, host->len);
}
if (port != 0) {
char port_str[20];
int port_str_len = sprintf(port_str, ":%u", port);
mbuf_append(&out, port_str, port_str_len);
}
if (path != NULL && path->len > 0) {
if (normalize_path) {
struct mg_str npath = mg_strdup(*path);
if (npath.len != path->len) goto out;
if (!mg_normalize_uri_path(path, &npath)) {
free((void *) npath.p);
goto out;
}
mbuf_append(&out, npath.p, npath.len);
free((void *) npath.p);
} else {
mbuf_append(&out, path->p, path->len);
}
} else if (normalize_path) {
mbuf_append(&out, "/", 1);
}
if (query != NULL && query->len > 0) {
mbuf_append(&out, "?", 1);
mbuf_append(&out, query->p, query->len);
}
if (fragment != NULL && fragment->len > 0) {
mbuf_append(&out, "#", 1);
mbuf_append(&out, fragment->p, fragment->len);
}
result = 0;
out:
if (result == 0) {
uri->p = out.buf;
uri->len = out.len;
} else {
mbuf_free(&out);
uri->p = NULL;
uri->len = 0;
}
return result;
}
#ifdef MG_MODULE_LINES #ifdef MG_MODULE_LINES
#line 1 "mongoose/src/http.c" #line 1 "mongoose/src/http.c"
#endif #endif
......
...@@ -3843,6 +3843,21 @@ int mg_parse_uri(const struct mg_str uri, struct mg_str *scheme, ...@@ -3843,6 +3843,21 @@ int mg_parse_uri(const struct mg_str uri, struct mg_str *scheme,
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);
/*
* Assemble URI from parts. Any of the inputs can be NULL or zero-length mg_str.
*
* If normalize_path is true, path is normalized by resolving relative refs.
*
* Result is a heap-allocated string (uri->p must be free()d after use).
*
* Returns 0 on success, -1 on error.
*/
int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info,
const struct mg_str *host, unsigned int port,
const struct mg_str *path, const struct mg_str *query,
const struct mg_str *fragment, int normalize_path,
struct mg_str *uri);
int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out); int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out);
#ifdef __cplusplus #ifdef __cplusplus
......
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