@@ -130,10 +130,17 @@ class AudioServerT {
130
130
if (callback == nullptr ) {
131
131
LOGD (" copy data..." );
132
132
if (converter_ptr == nullptr ) {
133
- copier.copy ();
133
+ sent += copier.copy ();
134
134
} else {
135
- copier.copy (*converter_ptr);
135
+ sent += copier.copy (*converter_ptr);
136
136
}
137
+
138
+ if (max_bytes > 0 && sent >= max_bytes) {
139
+ LOGI (" range exhausted..." );
140
+ client_obj.stop ();
141
+ active = false ;
142
+ }
143
+
137
144
// if we limit the size of the WAV the encoder gets automatically
138
145
// closed when all has been sent
139
146
if (!client_obj) {
@@ -176,6 +183,8 @@ class AudioServerT {
176
183
Client client_obj;
177
184
char *password = nullptr ;
178
185
char *network = nullptr ;
186
+ size_t max_bytes = 0 ;
187
+ size_t sent = 0 ;
179
188
180
189
// Content
181
190
const char *content_type = nullptr ;
@@ -211,10 +220,17 @@ class AudioServerT {
211
220
212
221
virtual void sendReplyHeader () {
213
222
TRACED ();
223
+
214
224
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
215
225
// and a content-type so the client knows what's coming, then a blank line:
216
- client_obj.println (" HTTP/1.1 200 OK" );
217
- LOGI (" Reply: HTTP/1.1 200 OK" );
226
+ char *response;
227
+ if (max_bytes > 0 ) {
228
+ response = " HTTP/1.1 206 OK" ;
229
+ } else {
230
+ response = " HTTP/1.1 200 OK" ;
231
+ }
232
+ client_obj.println (response);
233
+ LOGI (response);
218
234
if (content_type != nullptr ) {
219
235
client_obj.print (" Content-type:" );
220
236
client_obj.println (content_type);
@@ -250,18 +266,29 @@ class AudioServerT {
250
266
LOGI (" New Client:" ); // print a message out the serial port
251
267
String currentLine =
252
268
" " ; // make a String to hold incoming data from the client
269
+ long firstbyte = 0 ;
270
+ long lastbyte = 0 ;
253
271
while (client_obj.connected ()) { // loop while the client's connected
254
272
if (client_obj
255
273
.available ()) { // if there's bytes to read from the client,
256
274
char c = client_obj.read (); // read a byte, then
257
275
if (c == ' \n ' ) { // if the byte is a newline character
258
276
LOGI (" Request: %s" , currentLine.c_str ());
277
+ if (currentLine.startsWith (String (" Range: bytes=" ))) {
278
+ int minuspos = currentLine.indexOf (' -' , 13 );
279
+
280
+ // toInt returns 0 if it's an invalid conversion, so it's "safe"
281
+ firstbyte = currentLine.substring (13 , minuspos).toInt ();
282
+ lastbyte = currentLine.substring (minuspos + 1 ).toInt ();
283
+ }
259
284
// if the current line is blank, you got two newline characters in a
260
285
// row. that's the end of the client HTTP request, so send a
261
286
// response:
262
287
if (currentLine.length () == 0 ) {
263
288
sendReplyHeader ();
264
289
sendReplyContent ();
290
+ max_bytes = lastbyte - firstbyte;
291
+ sent = 0 ;
265
292
// break out of the while loop:
266
293
break ;
267
294
} else { // if you got a newline, then clear currentLine:
0 commit comments