Skip to content

Commit 784515d

Browse files
committed
move string tools to another source file
1 parent 4f4b5ad commit 784515d

File tree

4 files changed

+193
-129
lines changed

4 files changed

+193
-129
lines changed

ESPWebDAV.cpp

Lines changed: 2 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
2828
*/
2929

30+
#include "strutils.h"
31+
3032
#include <FS.h>
3133
#if defined(ARDUINO_ARCH_ESP8266) || defined(CORE_MOCK)
3234
#include <ESP8266WiFi.h>
@@ -76,10 +78,6 @@ const char * FileName(const char * path)
7678
#include <time.h>
7779
#include <ESPWebDAV.h>
7880

79-
// define cal constants
80-
const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
81-
const char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
82-
8381
#define ALLOW "PROPPATCH,PROPFIND,OPTIONS,DELETE" SCUNLOCK ",COPY" SCLOCK ",MOVE,HEAD,POST,PUT,GET"
8482

8583
#if WEBDAV_LOCK_SUPPORT
@@ -129,16 +127,6 @@ static const __FlashStringHelper* streamError (Stream::Report r)
129127
#define BUF_FREE() do { (void)0; } while (0)
130128
#endif
131129

132-
void ESPWebDAVCore::stripSlashes(String& name)
133-
{
134-
size_t i = 0;
135-
while (i < name.length())
136-
if (name[i] == '/' && name.length() > 1 && ((i == name.length() - 1) || name[i + 1] == '/'))
137-
name.remove(i, 1);
138-
else
139-
i++;
140-
}
141-
142130
#if WEBDAV_LOCK_SUPPORT
143131

144132

@@ -825,17 +813,6 @@ void ESPWebDAVCore::sendContentProp(const String& what, const String& response)
825813
}
826814

827815

828-
String ESPWebDAVCore::date2date(time_t date)
829-
{
830-
// get & convert time to required format
831-
// Tue, 13 Oct 2015 17:07:35 GMT
832-
tm* gTm = gmtime(&date);
833-
char buf[40];
834-
snprintf(buf, sizeof(buf), "%s, %02d %s %04d %02d:%02d:%02d GMT", wdays[gTm->tm_wday], gTm->tm_mday, months[gTm->tm_mon], gTm->tm_year + 1900, gTm->tm_hour, gTm->tm_min, gTm->tm_sec);
835-
return buf;
836-
}
837-
838-
839816
void ESPWebDAVCore::sendPropResponse(bool isDir, const String& fullResPathFS, size_t size, time_t lastWrite, time_t creationDate)
840817
{
841818
String fullResPath = fullResPathFS;
@@ -1197,33 +1174,6 @@ void ESPWebDAVCore::handleDirectoryCreate(ResourceType resource)
11971174
}
11981175

11991176

1200-
String ESPWebDAVCore::urlToUri(const String& url)
1201-
{
1202-
int index;
1203-
if (url.startsWith("http") && (index = url.indexOf("://")) <= 5)
1204-
{
1205-
int uriStart = url.indexOf('/', index + 3);
1206-
return url.substring(uriStart);
1207-
}
1208-
return url;
1209-
}
1210-
1211-
void ESPWebDAVCore::replaceFront (String& str, const String& from, const String& to)
1212-
{
1213-
if (from.length() && to.length() && str.indexOf(from) == 0)
1214-
{
1215-
DBG_PRINT("replaceFront(%s, %s): %s -> ", from.c_str(), to.c_str(), str.c_str());
1216-
String repl;
1217-
repl.reserve(str.length() + to.length() - from.length() + 1);
1218-
repl = to;
1219-
size_t skip = from.length() == 1? 0: from.length();
1220-
repl += str.c_str() + skip;
1221-
str = repl;
1222-
stripSlashes(str);
1223-
DBG_PRINT("%s", str.c_str());
1224-
}
1225-
}
1226-
12271177
void ESPWebDAVCore::handleMove(ResourceType resource, File& src)
12281178
{
12291179
const char* successCode = "201 Created";
@@ -1835,71 +1785,3 @@ void ESPWebDAVCore::processRange(const String& range)
18351785
}
18361786

18371787

1838-
int ESPWebDAVCore::htoi(char c)
1839-
{
1840-
c = tolower(c);
1841-
return c >= '0' && c <= '9' ? c - '0' :
1842-
c >= 'a' && c <= 'f' ? c - 'a' + 10 :
1843-
-1;
1844-
}
1845-
1846-
1847-
char ESPWebDAVCore::itoH(int c)
1848-
{
1849-
return c <= 9 ? c + '0' : c - 10 + 'A';
1850-
}
1851-
1852-
1853-
int ESPWebDAVCore::hhtoi(const char* c)
1854-
{
1855-
int h = htoi(*c);
1856-
int l = htoi(*(c + 1));
1857-
return h < 0 || l < 0 ? -1 : (h << 4) + l;
1858-
}
1859-
1860-
1861-
String ESPWebDAVCore::enc2c(const String& encoded)
1862-
{
1863-
String ret = encoded;
1864-
for (size_t i = 0; ret.length() >= 2 && i < ret.length() - 2; i++)
1865-
if (ret[i] == '%')
1866-
{
1867-
int v = hhtoi(ret.c_str() + i + 1);
1868-
if (v > 0)
1869-
{
1870-
ret[i] = v < 128 ? (char)v : '=';
1871-
ret.remove(i + 1, 2);
1872-
}
1873-
}
1874-
return ret;
1875-
}
1876-
1877-
1878-
static inline bool notEncodable (char c)
1879-
{
1880-
return c > 32 && c < 127;
1881-
}
1882-
1883-
String ESPWebDAVCore::c2enc(const String& decoded)
1884-
{
1885-
size_t l = decoded.length();
1886-
for (size_t i = 0; i < decoded.length(); i++)
1887-
if (!notEncodable(decoded[i]))
1888-
l += 2;
1889-
1890-
String ret;
1891-
ret.reserve(l);
1892-
for (size_t i = 0; i < decoded.length(); i++)
1893-
{
1894-
char c = decoded[i];
1895-
if (notEncodable(c))
1896-
ret += c;
1897-
else
1898-
{
1899-
ret += '%';
1900-
ret += itoH(c >> 4);
1901-
ret += itoH(c & 0xf);
1902-
}
1903-
}
1904-
return ret;
1905-
}

ESPWebDAV.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848
#define DBG_WEBDAV 1
4949
#endif
5050

51-
#if !defined(DBG_WEBDAV) && defined(DEBUG_ESP_PORT) && !defined(NDEBUG)
52-
#define DBG_WEBDAV 1
51+
#if defined(DEBUG_ESP_PORT)
5352
#define DBG_WEBDAV_PORT DEBUG_ESP_PORT
5453
#endif
5554

@@ -137,12 +136,6 @@ class ESPWebDAVCore
137136
void setDAVRoot (const String& davRoot) { _davRoot = davRoot; }
138137
void setFsRoot (const String& fsRoot) { _fsRoot = fsRoot; }
139138

140-
static void stripSlashes(String& name);
141-
static String date2date(time_t date);
142-
static String enc2c(const String& encoded);
143-
static String c2enc(const String& decoded);
144-
static void replaceFront (String& str, const String& from, const String& to);
145-
146139
protected:
147140

148141
static int htoi(char c);
@@ -194,7 +187,6 @@ class ESPWebDAVCore
194187
bool getPayload(StreamString& payload);
195188
void stripName(String& name);
196189
void stripHost(String& name);
197-
String urlToUri(const String& url);
198190

199191
enum virt_e { VIRT_NONE, VIRT_PROC };
200192
virt_e isVirtual(const String& uri);

strutils.cpp

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
2+
#include "strutils.h"
3+
4+
#include <StreamString.h>
5+
6+
// define cal constants
7+
const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
8+
const char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
9+
10+
int htoi(char c)
11+
{
12+
c = tolower(c);
13+
return c >= '0' && c <= '9' ? c - '0' :
14+
c >= 'a' && c <= 'f' ? c - 'a' + 10 :
15+
-1;
16+
}
17+
18+
19+
char itoH(int c)
20+
{
21+
return c <= 9 ? c + '0' : c - 10 + 'A';
22+
}
23+
24+
25+
int hhtoi(const char* c)
26+
{
27+
int h = htoi(*c);
28+
int l = htoi(*(c + 1));
29+
return h < 0 || l < 0 ? -1 : (h << 4) + l;
30+
}
31+
32+
33+
bool notEncodable (char c)
34+
{
35+
return c > 32 && c < 127;
36+
}
37+
38+
39+
void stripSlashes(String& name)
40+
{
41+
size_t i = 0;
42+
while (i < name.length())
43+
if (name[i] == '/' && name.length() > 1 && ((i == name.length() - 1) || name[i + 1] == '/'))
44+
name.remove(i, 1);
45+
else
46+
i++;
47+
}
48+
49+
50+
#if STREAMSEND_API
51+
52+
String date2date(time_t date)
53+
{
54+
// get & convert time to required format
55+
// Tue, 13 Oct 2015 17:07:35 GMT
56+
tm* gTm = gmtime(&date);
57+
String ret;
58+
ret.reserve(40);
59+
S2Stream(ret).printf("%s, %02d %s %04d %02d:%02d:%02d GMT",
60+
wdays[gTm->tm_wday],
61+
gTm->tm_mday,
62+
months[gTm->tm_mon],
63+
gTm->tm_year + 1900,
64+
gTm->tm_hour,
65+
gTm->tm_min,
66+
gTm->tm_sec);
67+
return ret;
68+
}
69+
70+
#else // !STREAMSEND_API
71+
72+
String date2date(time_t date)
73+
{
74+
// get & convert time to required format
75+
// Tue, 13 Oct 2015 17:07:35 GMT
76+
tm* gTm = gmtime(&date);
77+
char buf[40];
78+
snprintf(buf, sizeof(buf), "%s, %02d %s %04d %02d:%02d:%02d GMT",
79+
wdays[gTm->tm_wday],
80+
gTm->tm_mday,
81+
months[gTm->tm_mon],
82+
gTm->tm_year + 1900,
83+
gTm->tm_hour,
84+
gTm->tm_min,
85+
gTm->tm_sec);
86+
return buf;
87+
}
88+
89+
#endif // !STREAMSEND_API
90+
91+
92+
String OLDenc2c(const String& encoded)
93+
{
94+
String ret = encoded;
95+
for (size_t i = 0; ret.length() >= 2 && i < ret.length() - 2; i++)
96+
if (ret[i] == '%')
97+
{
98+
int v = hhtoi(ret.c_str() + i + 1);
99+
if (v > 0)
100+
{
101+
ret[i] = v < 128 ? (char)v : '=';
102+
ret.remove(i + 1, 2);
103+
}
104+
}
105+
return ret;
106+
}
107+
108+
109+
String enc2c(const String& encoded)
110+
{
111+
int v;
112+
String ret;
113+
ret.reserve(encoded.length());
114+
for (size_t i = 0; i < encoded.length(); i++)
115+
{
116+
if ( encoded[i] == '%'
117+
&& (i + 3) <= encoded.length()
118+
&& (v = hhtoi(encoded.c_str() + i + 1)) > 0)
119+
{
120+
ret += v;
121+
i += 2;
122+
}
123+
else
124+
ret += encoded[i];
125+
}
126+
return ret;
127+
}
128+
129+
130+
String c2enc(const String& decoded)
131+
{
132+
size_t l = decoded.length();
133+
for (size_t i = 0; i < decoded.length(); i++)
134+
if (!notEncodable(decoded[i]))
135+
l += 2;
136+
137+
String ret;
138+
ret.reserve(l);
139+
for (size_t i = 0; i < decoded.length(); i++)
140+
{
141+
char c = decoded[i];
142+
if (notEncodable(c))
143+
ret += c;
144+
else
145+
{
146+
ret += '%';
147+
ret += itoH(c >> 4);
148+
ret += itoH(c & 0xf);
149+
}
150+
}
151+
return ret;
152+
}
153+
154+
155+
void replaceFront (String& str, const String& from, const String& to)
156+
{
157+
if (from.length() && to.length() && str.indexOf(from) == 0)
158+
{
159+
String repl;
160+
repl.reserve(str.length() + to.length() - from.length() + 1);
161+
repl = to;
162+
size_t skip = from.length() == 1? 0: from.length();
163+
repl += str.c_str() + skip;
164+
str = repl;
165+
stripSlashes(str);
166+
}
167+
}
168+
169+
String urlToUri(const String& url)
170+
{
171+
int index;
172+
if (url.startsWith("http") && (index = url.indexOf("://")) <= 5)
173+
{
174+
int uriStart = url.indexOf('/', index + 3);
175+
return url.substring(uriStart);
176+
}
177+
return url;
178+
}
179+

strutils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#pragma once
3+
4+
#include <Arduino.h>
5+
6+
void stripSlashes(String& name);
7+
String date2date(time_t date);
8+
String enc2c(const String& encoded);
9+
String c2enc(const String& decoded);
10+
void replaceFront (String& str, const String& from, const String& to);
11+
String urlToUri(const String& url);

0 commit comments

Comments
 (0)