@@ -6633,8 +6633,7 @@ void mg_rpc_add(struct mg_rpc **head, struct mg_str method,
void (*fn)(struct mg_rpc_req *), void *fn_data) {
struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc));
if (rpc != NULL) {
- rpc->method.buf = mg_mprintf("%.*s", method.len, method.buf);
- rpc->method.len = method.len;
+ rpc->method = mg_strdup(method);
rpc->fn = fn;
rpc->fn_data = fn_data;
rpc->next = *head, *head = rpc;
@@ -8048,6 +8047,20 @@ int mg_casecmp(const char *s1, const char *s2) {
return diff;
}
+struct mg_str mg_strdup(const struct mg_str s) {
+ struct mg_str r = {NULL, 0};
+ if (s.len > 0 && s.buf != NULL) {
+ char *sc = (char *) calloc(1, s.len + 1);
+ if (sc != NULL) {
+ memcpy(sc, s.buf, s.len);
+ sc[s.len] = '\0';
+ r.buf = sc;
+ r.len = s.len;
+ }
+ }
+ return r;
+}
+
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
size_t i = 0;
while (i < str1.len && i < str2.len) {
@@ -10605,8 +10618,7 @@ static int mg_parse_pem(const struct mg_str pem, const struct mg_str label,
const char *c;
struct mg_str caps[5];
if (!mg_match(pem, mg_str("#-----BEGIN #-----#-----END #-----#"), caps)) {
- der->buf = mg_mprintf("%.*s", pem.len, pem.buf);
- der->len = pem.len;
+ *der = mg_strdup(pem);
return 0;
}
if (mg_strcmp(caps[1], label) != 0 || mg_strcmp(caps[3], label) != 0) {
@@ -186,7 +186,7 @@ extern "C" {
#define calloc(a, b) mg_calloc(a, b)
#define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a)
-#define strdup(s) mg_mprintf("%s", s)
+#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
static inline void *mg_calloc(size_t cnt, size_t size) {
@@ -288,7 +288,7 @@ extern uint32_t rt_time_get(void);
#include "cmsis_os2.h" // keep this include
#endif
-#define strdup(s) mg_mprintf("%s", s)
+#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)
#if defined(__ARMCC_VERSION)
#define mode_t size_t
@@ -861,6 +861,7 @@ struct mg_str mg_str_n(const char *s, size_t n);
int mg_casecmp(const char *s1, const char *s2);
int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2);
+struct mg_str mg_strdup(const struct mg_str s);
bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
bool mg_span(struct mg_str s, struct mg_str *a, struct mg_str *b, char delim);