This commit is contained in:
Sergey Lyubka 2022-07-01 17:28:06 +01:00
parent a183621268
commit f1da8542ce
20 changed files with 136 additions and 120 deletions

View File

@ -2840,7 +2840,7 @@ mg_json_get_bool(mg_str("[true]", "$[0]", &b)); // b is true
### mg\_json\_get\_long()
```c
long mg_json_get_bool(struct mg_str json, const char *path, long default_val);
long mg_json_get_long(struct mg_str json, const char *path, long default_val);
```
Fetch integer numeric (long) value from the json string `json` at JSON path
@ -2856,8 +2856,8 @@ Return value: found value, or `default_val` value
Usage example:
```c
long a = mg_json_get_bool(mg_str("[123]", "$a", -1)); // a = -1
long b = mg_json_get_bool(mg_str("[123]", "$[0]", -1)); // b = 123
long a = mg_json_get_long(mg_str("[123]", "$a", -1)); // a = -1
long b = mg_json_get_long(mg_str("[123]", "$[0]", -1)); // b = 123
```
### mg\_json\_get\_str()

View File

@ -23,7 +23,7 @@
#line 1 "src/base64.c"
#endif
#include <string.h>
static int mg_b64idx(int c) {
if (c < 26) {
@ -907,11 +907,60 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) {
return result;
}
#ifdef MG_ENABLE_LINES
#line 1 "src/fs_dummy.c"
#endif
static int d_stat(const char *path, size_t *size, time_t *mtime) {
(void) path, (void) size, (void) mtime;
return 0;
}
static void d_list(const char *path, void (*fn)(const char *, void *),
void *userdata) {
(void) path, (void) fn, (void) userdata;
}
static void *d_open(const char *path, int flags) {
(void) path, (void) flags;
return NULL;
}
static void d_close(void *fp) {
(void) fp;
}
static size_t d_read(void *fd, void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t d_write(void *fd, const void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t d_seek(void *fd, size_t offset) {
(void) fd, (void) offset;
return (size_t) ~0;
}
static bool d_rename(const char *from, const char *to) {
(void) from, (void) to;
return false;
}
static bool d_remove(const char *path) {
(void) path;
return false;
}
static bool d_mkdir(const char *path) {
(void) path;
return false;
}
struct mg_fs mg_fs_dummy = {d_stat, d_list, d_open, d_close, d_read,
d_write, d_seek, d_rename, d_remove, d_mkdir};
#ifdef MG_ENABLE_LINES
#line 1 "src/fs_fat.c"
#endif
#if MG_ENABLE_FATFS
#include <ff.h>
@ -1364,47 +1413,6 @@ static bool p_mkdir(const char *path) {
return mkdir(path, 0775) == 0;
}
#else
static int p_stat(const char *path, size_t *size, time_t *mtime) {
(void) path, (void) size, (void) mtime;
return 0;
}
static void p_list(const char *path, void (*fn)(const char *, void *),
void *userdata) {
(void) path, (void) fn, (void) userdata;
}
static void *p_open(const char *path, int flags) {
(void) path, (void) flags;
return NULL;
}
static void p_close(void *fp) {
(void) fp;
}
static size_t p_read(void *fd, void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t p_write(void *fd, const void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t p_seek(void *fd, size_t offset) {
(void) fd, (void) offset;
return (size_t) ~0;
}
static bool p_rename(const char *from, const char *to) {
(void) from, (void) to;
return false;
}
static bool p_remove(const char *path) {
(void) path;
return false;
}
static bool p_mkdir(const char *path) {
(void) path;
return false;
}
#endif
struct mg_fs mg_fs_posix = {p_stat, p_list, p_open, p_close, p_read,
@ -1890,11 +1898,12 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
opts->extra_headers ? opts->extra_headers : "");
} else {
int n, status = 200;
char range[100] = "";
char range[100];
int64_t r1 = 0, r2 = 0, cl = (int64_t) size;
// Handle Range header
struct mg_str *rh = mg_http_get_header(hm, "Range");
range[0] = '\0';
if (rh != NULL && (n = getrange(rh, &r1, &r2)) > 0 && r1 >= 0 && r2 >= 0) {
// If range is specified like "400-", set second limit to content len
if (n == 1) r2 = cl - 1;
@ -2095,7 +2104,7 @@ static int uri_to_path(struct mg_connection *c, struct mg_http_message *hm,
void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
const struct mg_http_serve_opts *opts) {
char path[MG_PATH_MAX] = "";
char path[MG_PATH_MAX];
const char *sp = opts->ssi_pattern;
int flags = uri_to_path(c, hm, opts, path, sizeof(path));
if (flags < 0) {
@ -2255,7 +2264,7 @@ void mg_http_delete_chunk(struct mg_connection *c, struct mg_http_message *hm) {
long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
struct mg_fs *fs, const char *path, size_t max_size) {
char buf[20] = "";
char buf[20] = "0";
long res = 0, offset;
mg_http_get_var(&hm->query, "offset", buf, sizeof(buf));
offset = strtol(buf, NULL, 0);
@ -2359,7 +2368,6 @@ struct mg_connection *mg_http_listen(struct mg_mgr *mgr, const char *url,
#include <string.h>
// Not using memset for zeroing memory, cause it can be dropped by compiler
// See https://github.com/cesanta/mongoose/pull/1265
@ -2696,6 +2704,7 @@ long mg_json_get_long(struct mg_str json, const char *path, long dflt) {
static void default_logger(unsigned char c) {
putchar(c);
(void) c;
}
static const char *s_spec = "2";
@ -2786,7 +2795,7 @@ void mg_hexdump(const void *buf, size_t len) {
#ifdef MG_ENABLE_LINES
#line 1 "src/md5.c"
#endif
#include <string.h>
#if defined(MG_ENABLE_MD5) && MG_ENABLE_MD5
@ -2804,7 +2813,7 @@ static void mg_byte_reverse(unsigned char *buf, unsigned longs) {
/* Forrest: MD5 expect LITTLE_ENDIAN, swap if BIG_ENDIAN */
#if BYTE_ORDER == BIG_ENDIAN
do {
uint32_t t = (uint32_t)((unsigned) buf[3] << 8 | buf[2]) << 16 |
uint32_t t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(uint32_t *) buf = t;
buf += 4;
@ -3552,7 +3561,7 @@ void mg_mgr_init(struct mg_mgr *mgr) {
/* Copyright(c) By Steve Reid <steve@edmweb.com> */
/* 100% Public Domain */
#include <string.h>
/*
* clang with std=-c99 uses __LITTLE_ENDIAN, by default
@ -3964,7 +3973,7 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
} else if (n > 0) {
if (c->is_hexdumping) {
union usa usa;
char t1[50] = "", t2[50] = "";
char t1[50], t2[50];
socklen_t slen = sizeof(usa.sin);
struct mg_addr a;
memset(&usa, 0, sizeof(usa));
@ -4493,12 +4502,14 @@ static char *mg_ssi(const char *path, const char *root, int depth) {
struct mg_iobuf b = {NULL, 0, 0};
FILE *fp = fopen(path, "rb");
if (fp != NULL) {
char buf[MG_SSI_BUFSIZ] = "", arg[sizeof(buf)] = "";
char buf[MG_SSI_BUFSIZ], arg[sizeof(buf)];
int ch, intag = 0;
size_t len = 0, align = MG_IO_SIZE;
buf[0] = arg[0] = '\0';
while ((ch = fgetc(fp)) != EOF) {
if (intag && ch == '>' && buf[len - 1] == '-' && buf[len - 2] == '-') {
buf[len++] = (char) (ch & 0xff);
buf[len] = '\0';
if (sscanf(buf, "<!--#include file=\"%[^\"]", arg)) {
char tmp[MG_PATH_MAX + MG_SSI_BUFSIZ + 10],
*p = (char *) path + strlen(path), *data;
@ -4577,7 +4588,6 @@ void mg_http_serve_ssi(struct mg_connection *c, const char *root,
#line 1 "src/str.c"
#endif
#include <stdlib.h>
struct mg_str mg_str_s(const char *s) {
struct mg_str str = {s, s == NULL ? 0 : strlen(s)};
@ -5278,7 +5288,6 @@ long mg_tls_send(struct mg_connection *c, const void *buf, size_t len) {
#line 1 "src/url.c"
#endif
#include <stdlib.h>
struct url {
size_t key, user, pass, host, port, uri, end;
@ -5369,10 +5378,6 @@ struct mg_str mg_url_pass(const char *url) {
#endif
#if MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
#include <mach/mach_time.h>
#endif
#if MG_ENABLE_CUSTOM_RANDOM
#else
void mg_random(void *buf, size_t len) {

View File

@ -413,6 +413,10 @@ extern int SockSet(SOCKET hSock, int Type, int Prop, void *pbuf, int size);
#define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors
#if defined(__APPLE__)
#include <mach/mach_time.h>
#endif
#if !defined(MG_ENABLE_POLL) && (defined(__linux__) || defined(__APPLE__))
#define MG_ENABLE_POLL 1
#endif
@ -817,6 +821,7 @@ struct mg_fs {
bool (*mkd)(const char *path); // Create directory
};
extern struct mg_fs mg_fs_dummy; // Dummy FS, does nothing
extern struct mg_fs mg_fs_posix; // POSIX open/close/read/write/seek
extern struct mg_fs mg_fs_packed; // Packed FS, see examples/device-dashboard
extern struct mg_fs mg_fs_fat; // FAT FS
@ -878,7 +883,7 @@ struct mg_str mg_url_pass(const char *url);
const char *mg_url_uri(const char *url);
#include <stddef.h>
struct mg_iobuf {
unsigned char *buf; // Pointer to stored data

View File

@ -4,6 +4,10 @@
#define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors
#if defined(__APPLE__)
#include <mach/mach_time.h>
#endif
#if !defined(MG_ENABLE_POLL) && (defined(__linux__) || defined(__APPLE__))
#define MG_ENABLE_POLL 1
#endif

View File

@ -1,5 +1,5 @@
#include "base64.h"
#include <string.h>
#include "arch.h"
static int mg_b64idx(int c) {
if (c < 26) {

View File

@ -25,6 +25,7 @@ struct mg_fs {
bool (*mkd)(const char *path); // Create directory
};
extern struct mg_fs mg_fs_dummy; // Dummy FS, does nothing
extern struct mg_fs mg_fs_posix; // POSIX open/close/read/write/seek
extern struct mg_fs mg_fs_packed; // Packed FS, see examples/device-dashboard
extern struct mg_fs mg_fs_fat; // FAT FS

44
src/fs_dummy.c Normal file
View File

@ -0,0 +1,44 @@
#include "fs.h"
static int d_stat(const char *path, size_t *size, time_t *mtime) {
(void) path, (void) size, (void) mtime;
return 0;
}
static void d_list(const char *path, void (*fn)(const char *, void *),
void *userdata) {
(void) path, (void) fn, (void) userdata;
}
static void *d_open(const char *path, int flags) {
(void) path, (void) flags;
return NULL;
}
static void d_close(void *fp) {
(void) fp;
}
static size_t d_read(void *fd, void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t d_write(void *fd, const void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t d_seek(void *fd, size_t offset) {
(void) fd, (void) offset;
return (size_t) ~0;
}
static bool d_rename(const char *from, const char *to) {
(void) from, (void) to;
return false;
}
static bool d_remove(const char *path) {
(void) path;
return false;
}
static bool d_mkdir(const char *path) {
(void) path;
return false;
}
struct mg_fs mg_fs_dummy = {d_stat, d_list, d_open, d_close, d_read,
d_write, d_seek, d_rename, d_remove, d_mkdir};

View File

@ -1,3 +1,4 @@
#include "arch.h"
#include "fs.h"
#if MG_ENABLE_FATFS

View File

@ -202,47 +202,6 @@ static bool p_mkdir(const char *path) {
return mkdir(path, 0775) == 0;
}
#else
static int p_stat(const char *path, size_t *size, time_t *mtime) {
(void) path, (void) size, (void) mtime;
return 0;
}
static void p_list(const char *path, void (*fn)(const char *, void *),
void *userdata) {
(void) path, (void) fn, (void) userdata;
}
static void *p_open(const char *path, int flags) {
(void) path, (void) flags;
return NULL;
}
static void p_close(void *fp) {
(void) fp;
}
static size_t p_read(void *fd, void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t p_write(void *fd, const void *buf, size_t len) {
(void) fd, (void) buf, (void) len;
return 0;
}
static size_t p_seek(void *fd, size_t offset) {
(void) fd, (void) offset;
return (size_t) ~0;
}
static bool p_rename(const char *from, const char *to) {
(void) from, (void) to;
return false;
}
static bool p_remove(const char *path) {
(void) path;
return false;
}
static bool p_mkdir(const char *path) {
(void) path;
return false;
}
#endif
struct mg_fs mg_fs_posix = {p_stat, p_list, p_open, p_close, p_read,

View File

@ -475,11 +475,12 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
opts->extra_headers ? opts->extra_headers : "");
} else {
int n, status = 200;
char range[100] = "";
char range[100];
int64_t r1 = 0, r2 = 0, cl = (int64_t) size;
// Handle Range header
struct mg_str *rh = mg_http_get_header(hm, "Range");
range[0] = '\0';
if (rh != NULL && (n = getrange(rh, &r1, &r2)) > 0 && r1 >= 0 && r2 >= 0) {
// If range is specified like "400-", set second limit to content len
if (n == 1) r2 = cl - 1;
@ -680,7 +681,7 @@ static int uri_to_path(struct mg_connection *c, struct mg_http_message *hm,
void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
const struct mg_http_serve_opts *opts) {
char path[MG_PATH_MAX] = "";
char path[MG_PATH_MAX];
const char *sp = opts->ssi_pattern;
int flags = uri_to_path(c, hm, opts, path, sizeof(path));
if (flags < 0) {
@ -840,7 +841,7 @@ void mg_http_delete_chunk(struct mg_connection *c, struct mg_http_message *hm) {
long mg_http_upload(struct mg_connection *c, struct mg_http_message *hm,
struct mg_fs *fs, const char *path, size_t max_size) {
char buf[20] = "";
char buf[20] = "0";
long res = 0, offset;
mg_http_get_var(&hm->query, "offset", buf, sizeof(buf));
offset = strtol(buf, NULL, 0);

View File

@ -1,8 +1,7 @@
#include "iobuf.h"
#include "arch.h"
#include "log.h"
#include <string.h>
// Not using memset for zeroing memory, cause it can be dropped by compiler
// See https://github.com/cesanta/mongoose/pull/1265
static void zeromem(volatile unsigned char *buf, size_t len) {

View File

@ -1,6 +1,6 @@
#pragma once
#include <stddef.h>
#include "arch.h"
struct mg_iobuf {
unsigned char *buf; // Pointer to stored data

View File

@ -3,6 +3,7 @@
static void default_logger(unsigned char c) {
putchar(c);
(void) c;
}
static const char *s_spec = "2";

View File

@ -1,5 +1,5 @@
#include <string.h>
#include "md5.h"
#include "arch.h"
#if defined(MG_ENABLE_MD5) && MG_ENABLE_MD5
#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER)
@ -16,7 +16,7 @@ static void mg_byte_reverse(unsigned char *buf, unsigned longs) {
/* Forrest: MD5 expect LITTLE_ENDIAN, swap if BIG_ENDIAN */
#if BYTE_ORDER == BIG_ENDIAN
do {
uint32_t t = (uint32_t)((unsigned) buf[3] << 8 | buf[2]) << 16 |
uint32_t t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(uint32_t *) buf = t;
buf += 4;

View File

@ -1,7 +1,7 @@
/* Copyright(c) By Steve Reid <steve@edmweb.com> */
/* 100% Public Domain */
#include "sha1.h"
#include <string.h>
#include "arch.h"
/*
* clang with std=-c99 uses __LITTLE_ENDIAN, by default

View File

@ -118,7 +118,7 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
} else if (n > 0) {
if (c->is_hexdumping) {
union usa usa;
char t1[50] = "", t2[50] = "";
char t1[50], t2[50];
socklen_t slen = sizeof(usa.sin);
struct mg_addr a;
memset(&usa, 0, sizeof(usa));

View File

@ -15,12 +15,14 @@ static char *mg_ssi(const char *path, const char *root, int depth) {
struct mg_iobuf b = {NULL, 0, 0};
FILE *fp = fopen(path, "rb");
if (fp != NULL) {
char buf[MG_SSI_BUFSIZ] = "", arg[sizeof(buf)] = "";
char buf[MG_SSI_BUFSIZ], arg[sizeof(buf)];
int ch, intag = 0;
size_t len = 0, align = MG_IO_SIZE;
buf[0] = arg[0] = '\0';
while ((ch = fgetc(fp)) != EOF) {
if (intag && ch == '>' && buf[len - 1] == '-' && buf[len - 2] == '-') {
buf[len++] = (char) (ch & 0xff);
buf[len] = '\0';
if (sscanf(buf, "<!--#include file=\"%[^\"]", arg)) {
char tmp[MG_PATH_MAX + MG_SSI_BUFSIZ + 10],
*p = (char *) path + strlen(path), *data;

View File

@ -1,5 +1,4 @@
#include "str.h"
#include <stdlib.h>
struct mg_str mg_str_s(const char *s) {
struct mg_str str = {s, s == NULL ? 0 : strlen(s)};

View File

@ -1,5 +1,4 @@
#include "url.h"
#include <stdlib.h>
struct url {
size_t key, user, pass, host, port, uri, end;

View File

@ -1,9 +1,5 @@
#include "util.h"
#if MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
#include <mach/mach_time.h>
#endif
#if MG_ENABLE_CUSTOM_RANDOM
#else
void mg_random(void *buf, size_t len) {