SimpleOTA client v1.2.0
Shared core of the SimpleOTA Arduino and ESP-IDF clients
Loading...
Searching...
No Matches
sota_url.h
Go to the documentation of this file.
1
11#ifndef SOTA_URL_H
12#define SOTA_URL_H
13
14#include <stdbool.h>
15#include <stddef.h>
16#include <string.h>
17
19#define SOTA_DEFAULT_SERVER_URL "https://simpleota.com"
21#define SOTA_SERVER_URL_MAX_LEN 256u
22
29
31static inline bool sota_url_has_scheme(const char *url, const char *scheme)
32{
33 for (; *scheme; ++url, ++scheme) {
34 char c = *url >= 'A' && *url <= 'Z' ? (char)(*url - 'A' + 'a') : *url;
35 if (c != *scheme) return false;
36 }
37 return true;
38}
39
42static inline bool sota_url_chars_ok(const char *url)
43{
44 for (; *url; ++url) {
45 unsigned char c = (unsigned char)*url;
46 if (c <= 0x20u || c == 0x7fu) return false;
47 }
48 return true;
49}
50
55static inline sota_url_status_t sota_validate_server_url(const char *url)
56{
57 if (!url || !url[0]) return SOTA_URL_MALFORMED;
58 size_t len = strlen(url);
60 if (!sota_url_chars_ok(url)) return SOTA_URL_MALFORMED;
61 if (sota_url_has_scheme(url, "http://") && !sota_url_has_scheme(url, "https://"))
62 return SOTA_URL_NOT_HTTPS;
63 if (!sota_url_has_scheme(url, "https://")) return SOTA_URL_MALFORMED;
64 const char *slash = strchr(url + 8, '/');
65 return (slash ? (size_t)(slash - url) : len) == 8u ? SOTA_URL_MALFORMED : SOTA_URL_OK;
66}
67
69static inline const char *sota_url_status_name(int status)
70{
71 if (status == SOTA_URL_NOT_HTTPS) return "not_https";
72 return status == SOTA_URL_OK ? "ok" : "malformed";
73}
74
81static inline sota_url_status_t sota_redirect_target(const char *base, const char *location,
82 char *out, size_t cap)
83{
84 if (!base || !location || !location[0] || !out || !sota_url_chars_ok(location)) {
85 return SOTA_URL_MALFORMED;
86 }
87 size_t origin = 0;
88 if (location[0] == '/' && location[1] != '/') {
89 if (!sota_url_has_scheme(base, "https://")) return SOTA_URL_NOT_HTTPS;
90 const char *slash = strchr(base + 8, '/');
91 origin = slash ? (size_t)(slash - base) : strlen(base);
92 } else if (sota_url_has_scheme(location, "http://")) {
93 return SOTA_URL_NOT_HTTPS;
94 } else if (!sota_url_has_scheme(location, "https://") || location[8] == '/' || !location[8]) {
95 return SOTA_URL_MALFORMED;
96 }
97 size_t len = strlen(location);
98 if (origin + len >= cap) return SOTA_URL_MALFORMED;
99 memcpy(out, base, origin);
100 memcpy(out + origin, location, len + 1);
101 return SOTA_URL_OK;
102}
103
108static inline bool sota_build_api_url(const char *base, const char *path, char *out,
109 size_t out_len)
110{
111 if (!base || !path || !out || out_len == 0) return false;
112 size_t n = strlen(base);
113 while (n > 8u && base[n - 1] == '/') --n;
114 size_t plen = strlen(path);
115 if (n >= out_len || plen > (out_len - 1u) - n) return false;
116 memcpy(out, base, n);
117 memcpy(out + n, path, plen + 1u);
118 return true;
119}
120
121#endif /* SOTA_URL_H */
static sota_url_status_t sota_redirect_target(const char *base, const char *location, char *out, size_t cap)
Where a download redirect from base leads, into out.
Definition sota_url.h:81
static bool sota_url_chars_ok(const char *url)
True when url has no spaces or control characters, which could split a request line.
Definition sota_url.h:42
static bool sota_build_api_url(const char *base, const char *path, char *out, size_t out_len)
base + path into out, with trailing slashes of base removed.
Definition sota_url.h:108
sota_url_status_t
Why a server URL was accepted or refused.
Definition sota_url.h:24
@ SOTA_URL_OK
Usable.
Definition sota_url.h:25
@ SOTA_URL_NOT_HTTPS
A plain http:// URL.
Definition sota_url.h:26
@ SOTA_URL_MALFORMED
Empty, too long, no host, or bad characters.
Definition sota_url.h:27
static sota_url_status_t sota_validate_server_url(const char *url)
An https:// URL with a host, at most SOTA_SERVER_URL_MAX_LEN characters, and no spaces or control cha...
Definition sota_url.h:55
static bool sota_url_has_scheme(const char *url, const char *scheme)
True when url starts with scheme, ignoring the case of the URL.
Definition sota_url.h:31
#define SOTA_SERVER_URL_MAX_LEN
Longest base URL accepted, without the terminating NUL.
Definition sota_url.h:21
static const char * sota_url_status_name(int status)
The status as a short token for logs: "ok", "not_https" or "malformed".
Definition sota_url.h:69