master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "first.h"
26/*
27 * curl's test suite Real Time Streaming Protocol (RTSP) server.
28 *
29 * This source file was started based on curl's HTTP test suite server.
30 */
32#ifdef HAVE_NETINET_TCP_H
33#include <netinet/tcp.h> /* for TCP_NODELAY */
34#endif
36static long rtspd_prevtestno = -1; /* previous test number we served */
37static long rtspd_prevpartno = -1; /* previous part number we served */
38static bool rtspd_prevbounce = FALSE; /* instructs the server to override the
39 requested part number to
40 prevpartno + 1 when prevtestno and
41 current test are the same */
43#define RCMD_NORMALREQ 0 /* default request, use the tests file normally */
44#define RCMD_IDLE 1 /* told to sit idle */
45#define RCMD_STREAM 2 /* told to stream */
47typedef enum {
48 RPROT_NONE = 0,
49 RPROT_RTSP = 1,
50 RPROT_HTTP = 2
51} reqprot_t;
53#define SET_RTP_PKT_CHN(p, c) ((p)[1] = (char)((c) & 0xFF))
55#define SET_RTP_PKT_LEN(p, l) (((p)[2] = (char)(((l) >> 8) & 0xFF)), \
56 ((p)[3] = (char)((l) & 0xFF)))
58struct rtspd_httprequest {
59 char reqbuf[150000]; /* buffer area for the incoming request */
60 size_t checkindex; /* where to start checking of the request */
61 size_t offset; /* size of the incoming request */
62 long testno; /* test number found in the request */
63 long partno; /* part number found in the request */
64 bool open; /* keep connection open info, as found in the request */
65 bool auth_req; /* authentication required, do not wait for body unless
66 there is an Authorization header */
67 bool auth; /* Authorization header present in the incoming request */
68 size_t cl; /* Content-Length of the incoming request */
69 bool digest; /* Authorization digest header found */
70 int pipe; /* if non-zero, expect this many requests to do a "piped"
71 request/response */
72 int skip; /* if non-zero, the server is instructed to not read this
73 many bytes from a PUT/POST request. Ie the client sends N
74 bytes said in Content-Length, but the server only reads N
75 - skip bytes. */
76 int rcmd; /* doing a special command, see defines above */
77 reqprot_t protocol; /* request protocol, HTTP or RTSP */
78 int prot_version; /* HTTP or RTSP version (major * 10 + minor) */
79 bool pipelining; /* true if request is pipelined */
80 char *rtp_buffer;
81 size_t rtp_buffersize;
82};
84#define RTSPDVERSION "curl test suite RTSP server/0.1"
86#define REQUEST_DUMP "server.input"
87#define RESPONSE_DUMP "server.response"
89/* very-big-path support */
90#define MAXDOCNAMELEN 140000
91#define MAXDOCNAMELEN_TXT "139999"
93#define REQUEST_KEYWORD_SIZE 256
94#define REQUEST_KEYWORD_SIZE_TXT "255"
96#define CMD_AUTH_REQUIRED "auth_required"
98/* 'idle' means that it accepts the request fine but never responds
99 any data. Keep the connection alive. */
100#define CMD_IDLE "idle"
102/* 'stream' means to send a never-ending stream of data */
103#define CMD_STREAM "stream"
105#define END_OF_HEADERS "\r\n\r\n"
107/* sent as reply to a QUIT */
108static const char docquit_rtsp[] = "HTTP/1.1 200 Goodbye" END_OF_HEADERS;
110/* sent as reply to a CONNECT */
111static const char docconnect[] =
112 "HTTP/1.1 200 Mighty fine indeed" END_OF_HEADERS;
114/* sent as reply to a "bad" CONNECT */
115static const char docbadconnect[] =
116 "HTTP/1.1 501 Forbidden you fool" END_OF_HEADERS;
118/* send back this on HTTP 404 file not found */
119static const char doc404_HTTP[] =
120 "HTTP/1.1 404 Not Found\r\n"
121 "Server: " RTSPDVERSION "\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html"
124 END_OF_HEADERS
125 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
126 "<HTML><HEAD>\n"
127 "<TITLE>404 Not Found</TITLE>\n"
128 "</HEAD><BODY>\n"
129 "<H1>Not Found</H1>\n"
130 "The requested URL was not found on this server.\n"
131 "<P><HR><ADDRESS>" RTSPDVERSION "</ADDRESS>\n"
132 "</BODY></HTML>\n";
134/* send back this on RTSP 404 file not found */
135static const char doc404_RTSP[] = "RTSP/1.0 404 Not Found\r\n"
136 "Server: " RTSPDVERSION
137 END_OF_HEADERS;
139/* Default size to send away fake RTP data */
140#define RTP_DATA_SIZE 12
141static const char RTP_DATA[] = "$_1234\n\0Rsdf";
143/* parse the file on disk that might have a test number for us */
144static int rtspd_cmdfile(struct rtspd_httprequest *req)
145{
146 FILE *f = curlx_fopen(cmdfile, FOPEN_READTEXT);
147 if(f) {
148 int testnum = DOCNUMBER_NOTHING;
149 char buf[256];
150 while(fgets(buf, sizeof(buf), f)) {
151 if(sscanf(buf, "Testnum %d", &testnum) == 1) {
152 logmsg("[%s] cmdfile says testnum %d", cmdfile, testnum);
153 req->testno = testnum;
154 }
155 }
156 curlx_fclose(f);
157 }
158 return 0;
159}
161static int rtspd_ProcessRequest(struct rtspd_httprequest *req)
162{
163 const char *line = &req->reqbuf[req->checkindex];
164 bool chunked = FALSE;
165 static char request[REQUEST_KEYWORD_SIZE];
166 static char doc[MAXDOCNAMELEN];
167 static char prot_str[5];
168 int prot_major, prot_minor;
169 const char *end = strstr(line, END_OF_HEADERS);
171 logmsg("rtspd_ProcessRequest() called with testno %ld and line [%s]",
172 req->testno, line);
174 /* try to figure out the request characteristics as soon as possible, but
175 only once! */
176 if((req->testno == DOCNUMBER_NOTHING) &&
177 sscanf(line,
178 "%" REQUEST_KEYWORD_SIZE_TXT"s %" MAXDOCNAMELEN_TXT "s %4s/%d.%d",
179 request,
180 doc,
181 prot_str,
182 &prot_major,
183 &prot_minor) == 5) {
184 const char *ptr;
185 const char *pval;
186 curl_off_t testnum;
188 if(!strcmp(prot_str, "HTTP")) {
189 req->protocol = RPROT_HTTP;
190 }
191 else if(!strcmp(prot_str, "RTSP")) {
192 req->protocol = RPROT_RTSP;
193 }
194 else {
195 req->protocol = RPROT_NONE;
196 logmsg("got unknown protocol %s", prot_str);
197 return 1;
198 }
200 req->prot_version = (prot_major * 10) + prot_minor;
202 /* find the last slash */
203 ptr = strrchr(doc, '/');
205 /* get the number after it */
206 if(ptr) {
207 FILE *stream;
208 if((strlen(doc) + strlen(request)) < 200)
209 logmsg("Got request: %s %s %s/%d.%d",
210 request, doc, prot_str, prot_major, prot_minor);
211 else
212 logmsg("Got a *HUGE* request %s/%d.%d",
213 prot_str, prot_major, prot_minor);
215 if(!strncmp("/verifiedserver", ptr, 15)) {
216 logmsg("Are-we-friendly question received");
217 req->testno = DOCNUMBER_WERULEZ;
218 return 1; /* done */
219 }
221 if(!strncmp("/quit", ptr, 5)) {
222 logmsg("Request-to-quit received");
223 req->testno = DOCNUMBER_QUIT;
224 return 1; /* done */
225 }
227 ptr++; /* skip the slash */
229 /* skip all non-numericals following the slash */
230 while(*ptr && !ISDIGIT(*ptr))
231 ptr++;
233 pval = ptr;
234 if(!curlx_str_number(&pval, &testnum, INT_MAX))
235 req->testno = (long)testnum;
236 else {
237 req->protocol = RPROT_NONE;
238 logmsg("rtspd: failed to read the test number from '%s'", doc);
239 return 1;
240 }
242 if(req->testno > 10000) {
243 req->partno = req->testno % 10000;
244 req->testno /= 10000;
245 }
246 else
247 req->partno = 0;
249 logmsg("Requested test number %ld part %ld", req->testno, req->partno);
251 stream = test2fopen(req->testno, logdir);
253 if(!stream) {
254 char errbuf[STRERROR_LEN];
255 int error = errno;
256 logmsg("fopen() failed with error (%d) %s",
257 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
258 logmsg("Could not open test file %ld", req->testno);
259 req->open = FALSE; /* closes connection */
260 return 1; /* done */
261 }
262 else {
263 char *cmd = NULL;
264 size_t cmdsize = 0;
265 int num = 0;
267 int rtp_channel = 0;
268 int rtp_size = 0;
269 int rtp_size_err = 0;
270 int rtp_partno = -1;
271 char *rtp_scratch = NULL;
273 /* get the custom server control "commands" */
274 int error = getpart(&cmd, &cmdsize, "reply", "servercmd", stream);
275 curlx_fclose(stream);
276 if(error) {
277 logmsg("getpart() failed with error (%d)", error);
278 req->open = FALSE; /* closes connection */
279 return 1; /* done */
280 }
281 ptr = cmd;
283 if(cmdsize) {
284 logmsg("Found a reply-servercmd section!");
285 do {
286 rtp_size_err = 0;
287 if(!strncmp(CMD_AUTH_REQUIRED, ptr,
288 CURL_CSTRLEN(CMD_AUTH_REQUIRED))) {
289 logmsg("instructed to require authorization header");
290 req->auth_req = TRUE;
291 }
292 else if(!strncmp(CMD_IDLE, ptr, CURL_CSTRLEN(CMD_IDLE))) {
293 logmsg("instructed to idle");
294 req->rcmd = RCMD_IDLE;
295 req->open = TRUE;
296 }
297 else if(!strncmp(CMD_STREAM, ptr, CURL_CSTRLEN(CMD_STREAM))) {
298 logmsg("instructed to stream");
299 req->rcmd = RCMD_STREAM;
300 }
301 else if(sscanf(ptr, "pipe: %d", &num) == 1) {
302 logmsg("instructed to allow a pipe size of %d", num);
303 if(num < 0)
304 logmsg("negative pipe size ignored");
305 else if(num > 0)
306 req->pipe = num - 1; /* decrease by one since we do not count
307 the first request in this number */
308 }
309 else if(sscanf(ptr, "skip: %d", &num) == 1) {
310 logmsg("instructed to skip this number of bytes %d", num);
311 req->skip = num;
312 }
313 else if(3 <= sscanf(ptr,
314 "rtp: part %d channel %d size %d size_err %d",
315 &rtp_partno, &rtp_channel, &rtp_size,
316 &rtp_size_err)) {
318 if(rtp_partno == req->partno) {
319 int i = 0;
320 logmsg("RTP: part %d channel %d size %d size_err %d",
321 rtp_partno, rtp_channel, rtp_size, rtp_size_err);
323 /* Make our scratch buffer enough to fit all the
324 * desired data and one for padding */
325 rtp_scratch = malloc(rtp_size + 4 + RTP_DATA_SIZE);
327 /* RTP is signalled with a $ */
328 rtp_scratch[0] = '$';
330 /* The channel follows and is one byte */
331 SET_RTP_PKT_CHN(rtp_scratch, rtp_channel);
333 /* Length follows and is a two byte short in network order */
334 SET_RTP_PKT_LEN(rtp_scratch, rtp_size + rtp_size_err);
336 /* Fill it with junk data */
337 for(i = 0; i < rtp_size; i += RTP_DATA_SIZE) {
338 /* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */
339 memcpy(rtp_scratch + 4 + i, RTP_DATA, RTP_DATA_SIZE);
340 }
342 if(!req->rtp_buffer) {
343 req->rtp_buffer = rtp_scratch;
344 req->rtp_buffersize = rtp_size + 4;
345 }
346 else {
347 char *rtp_buffer = realloc(req->rtp_buffer,
348 req->rtp_buffersize +
349 rtp_size + 4);
350 if(rtp_buffer) {
351 req->rtp_buffer = rtp_buffer;
352 memcpy(req->rtp_buffer + req->rtp_buffersize, rtp_scratch,
353 rtp_size + 4);
354 req->rtp_buffersize += rtp_size + 4;
355 }
356 else
357 logmsg("failed resizing buffer.");
358 free(rtp_scratch);
359 }
360 logmsg("rtp_buffersize is %lu, rtp_size is %d.",
361 (unsigned long)req->rtp_buffersize, rtp_size);
362 }
363 }
364 else {
365 logmsg("funny instruction found: %s", ptr);
366 }
368 ptr = strchr(ptr, '\n');
369 if(ptr)
370 ptr++;
371 else
372 ptr = NULL;
373 } while(ptr && *ptr);
374 logmsg("Done parsing server commands");
375 }
376 free(cmd);
377 }
378 }
379 else {
380 if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
381 doc, &prot_major, &prot_minor) == 3) {
382 logmsg("Received a CONNECT %s HTTP/%d.%d request",
383 doc, prot_major, prot_minor);
385 if(req->prot_version == 10)
386 req->open = FALSE; /* HTTP 1.0 closes connection by default */
388 if(!strncmp(doc, "bad", 3))
389 /* if the hostname starts with bad, we fake an error here */
390 req->testno = DOCNUMBER_BADCONNECT;
391 else if(!strncmp(doc, "test", 4)) {
392 /* if the hostname starts with test, the port number used in the
393 CONNECT line is used as test number! */
394 const char *portp = strchr(doc, ':');
395 if(portp && (*(portp + 1) != '\0') && ISDIGIT(*(portp + 1))) {
396 pval = portp + 1;
397 if(!curlx_str_number(&pval, &testnum, INT_MAX))
398 req->testno = (long)testnum;
399 else
400 req->testno = DOCNUMBER_CONNECT;
401 }
402 else
403 req->testno = DOCNUMBER_CONNECT;
404 }
405 else
406 req->testno = DOCNUMBER_CONNECT;
407 }
408 if(req->testno == DOCNUMBER_NOTHING)
409 /* might get the test number */
410 rtspd_cmdfile(req);
411 if(req->testno == DOCNUMBER_NOTHING) {
412 logmsg("Did not find test number");
413 req->testno = DOCNUMBER_404;
414 }
415 }
416 }
418 if(!end) {
419 /* we do not have a complete request yet! */
420 logmsg("rtspd_ProcessRequest returned without a complete request");
421 return 0; /* not complete yet */
422 }
423 logmsg("rtspd_ProcessRequest found a complete request");
425 if(req->pipe)
426 /* we do have a full set, advance the checkindex to after the end of the
427 headers, for the pipelining case mostly */
428 req->checkindex += (end - line) + CURL_CSTRLEN(END_OF_HEADERS);
430 /* **** Persistence ****
431 *
432 * If the request is an HTTP/1.0 one, we close the connection unconditionally
433 * when we are done.
434 *
435 * If the request is an HTTP/1.1 one, we MUST check for a "Connection:"
436 * header that might say "close". If it does, we close a connection when
437 * this request is processed. Otherwise, we keep the connection alive for X
438 * seconds.
439 */
441 do {
442 if(got_exit_signal)
443 return 1; /* done */
445 if((req->cl == 0) && !CURL_STRNICMP("Content-Length:", line, 15)) {
446 /* If we do not ignore content-length, we read it and we read the whole
447 request including the body before we return. If we have been told to
448 ignore the content-length, we return as soon as all headers
449 have been received */
450 curl_off_t clen;
451 const char *p = line + CURL_CSTRLEN("Content-Length:");
452 if(curlx_str_numblanks(&p, &clen)) {
453 /* this assumes that a zero Content-Length is valid */
454 logmsg("Found invalid '%s' in the request", line);
455 req->open = FALSE; /* closes connection */
456 return 1; /* done */
457 }
458 req->cl = (size_t)clen - req->skip;
460 logmsg("Found Content-Length: %lu in the request", (unsigned long)clen);
461 if(req->skip)
462 logmsg("... but going to abort after %lu bytes",
463 (unsigned long)req->cl);
464 break;
465 }
466 else if(!CURL_STRNICMP("Transfer-Encoding: chunked", line,
467 CURL_CSTRLEN("Transfer-Encoding: chunked"))) {
468 /* chunked data coming in */
469 chunked = TRUE;
470 }
472 if(chunked) {
473 if(strstr(req->reqbuf, "\r\n0\r\n\r\n"))
474 /* end of chunks reached */
475 return 1; /* done */
476 else
477 return 0; /* not done */
478 }
480 line = strchr(line, '\n');
481 if(line)
482 line++;
484 } while(line);
486 if(!req->auth && strstr(req->reqbuf, "Authorization:")) {
487 req->auth = TRUE; /* Authorization: header present! */
488 if(req->auth_req)
489 logmsg("Authorization header found, as required");
490 }
492 if(!req->digest && strstr(req->reqbuf, "Authorization: Digest")) {
493 /* If the client is passing this Digest-header, we set the part number
494 to 1000. Not only to spice up the complexity of this, but to make
495 Digest stuff to work in the test suite. */
496 req->partno += 1000;
497 req->digest = TRUE; /* header found */
498 logmsg("Received Digest request, sending back data %ld", req->partno);
499 }
500 else if((req->partno >= 1000) &&
501 strstr(req->reqbuf, "Authorization: Basic")) {
502 /* If the client is passing this Basic-header and the part number is
503 already >=1000, we add 1 to the part number. This allows simple Basic
504 authentication negotiation to work in the test suite. */
505 req->partno += 1;
506 logmsg("Received Basic request, sending back data %ld", req->partno);
507 }
508 if(strstr(req->reqbuf, "Connection: close"))
509 req->open = FALSE; /* close connection after this request */
511 if(!req->pipe &&
512 req->open &&
513 req->prot_version >= 11 &&
514 req->reqbuf + req->offset > end + CURL_CSTRLEN(END_OF_HEADERS) &&
515 (!strncmp(req->reqbuf, "GET", CURL_CSTRLEN("GET")) ||
516 !strncmp(req->reqbuf, "HEAD", CURL_CSTRLEN("HEAD")))) {
517 /* If we have a persistent connection, HTTP version >= 1.1
518 and GET/HEAD request, enable pipelining. */
519 req->checkindex = (end - req->reqbuf) + CURL_CSTRLEN(END_OF_HEADERS);
520 req->pipelining = TRUE;
521 }
523 while(req->pipe) {
524 if(got_exit_signal)
525 return 1; /* done */
526 /* scan for more header ends within this chunk */
527 line = &req->reqbuf[req->checkindex];
528 end = strstr(line, END_OF_HEADERS);
529 if(!end)
530 break;
531 req->checkindex += (end - line) + CURL_CSTRLEN(END_OF_HEADERS);
532 req->pipe--;
533 }
535 /* If authentication is required and no auth was provided, end now. This
536 makes the server NOT wait for PUT/POST data and you can then make the
537 test case send a rejection before any such data has been sent. Test case
538 154 uses this.*/
539 if(req->auth_req && !req->auth)
540 return 1; /* done */
542 if(req->cl > 0) {
543 if(end && (req->cl <= req->offset - (end - req->reqbuf) -
544 CURL_CSTRLEN(END_OF_HEADERS)))
545 return 1; /* done */
546 else
547 return 0; /* not complete yet */
548 }
550 return 1; /* done */
551}
553/* return 0 on success, non-zero on failure */
554static int rtspd_get_request(curl_socket_t sock, struct rtspd_httprequest *req)
555{
556 int sockerr;
557 char errbuf[STRERROR_LEN];
558 int fail = 0;
559 int done_processing = 0;
560 char *reqbuf = req->reqbuf;
561 ssize_t got = 0;
563 char *pipereq = NULL;
564 size_t pipereq_length = 0;
566 if(req->pipelining) {
567 pipereq = reqbuf + req->checkindex;
568 pipereq_length = req->offset - req->checkindex;
569 }
571 /* Init the httprequest structure properly for the upcoming request */
573 req->checkindex = 0;
574 req->offset = 0;
575 req->testno = DOCNUMBER_NOTHING;
576 req->partno = 0;
577 req->open = TRUE;
578 req->auth_req = FALSE;
579 req->auth = FALSE;
580 req->cl = 0;
581 req->digest = FALSE;
582 req->pipe = 0;
583 req->skip = 0;
584 req->rcmd = RCMD_NORMALREQ;
585 req->protocol = RPROT_NONE;
586 req->prot_version = 0;
587 req->pipelining = FALSE;
588 req->rtp_buffer = NULL;
589 req->rtp_buffersize = 0;
591 /* end of httprequest init */
593 while(!done_processing && (req->offset < sizeof(req->reqbuf) - 1)) {
594 if(pipereq_length && pipereq) {
595 memmove(reqbuf, pipereq, pipereq_length);
596 got = curlx_uztosz(pipereq_length);
597 pipereq_length = 0;
598 }
599 else {
600 if(req->skip)
601 /* we are instructed to not read the entire thing, so we make sure to
602 only read what we are supposed to and NOT read the entire thing the
603 client wants to send! */
604 got = sread(sock, reqbuf + req->offset, req->cl);
605 else
606 got = sread(sock, reqbuf + req->offset,
607 sizeof(req->reqbuf) - 1 - req->offset);
608 }
609 if(got_exit_signal)
610 return 1;
611 if(got == 0) {
612 logmsg("Connection closed by client");
613 fail = 1;
614 }
615 else if(got < 0) {
616 sockerr = SOCKERRNO;
617 logmsg("recv() returned error (%d) %s",
618 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
619 fail = 1;
620 }
621 if(fail) {
622 /* dump the request received so far to the external file */
623 reqbuf[req->offset] = '\0';
624 storerequest(reqbuf, req->offset, REQUEST_DUMP);
625 return 1;
626 }
628 logmsg("Read %ld bytes", (long)got);
630 req->offset += (size_t)got;
631 reqbuf[req->offset] = '\0';
633 done_processing = rtspd_ProcessRequest(req);
634 if(got_exit_signal)
635 return 1;
636 if(done_processing && req->pipe) {
637 logmsg("Waiting for another piped request");
638 done_processing = 0;
639 req->pipe--;
640 }
641 }
643 if((req->offset == sizeof(req->reqbuf) - 1) && (got > 0)) {
644 logmsg("Request would overflow buffer, closing connection");
645 /* dump request received so far to external file anyway */
646 reqbuf[sizeof(req->reqbuf) - 1] = '\0';
647 fail = 1;
648 }
649 else if(req->offset > sizeof(req->reqbuf) - 1) {
650 logmsg("Request buffer overflow, closing connection");
651 /* dump request received so far to external file anyway */
652 reqbuf[sizeof(req->reqbuf) - 1] = '\0';
653 fail = 1;
654 }
655 else
656 reqbuf[req->offset] = '\0';
658 /* dump the request to an external file */
659 storerequest(reqbuf, req->pipelining ? req->checkindex : req->offset,
660 REQUEST_DUMP);
661 if(got_exit_signal)
662 return 1;
664 return fail; /* return 0 on success */
665}
667/* returns -1 on failure */
668static int rtspd_send_doc(curl_socket_t sock, struct rtspd_httprequest *req)
669{
670 ssize_t written;
671 size_t count;
672 const char *buffer;
673 char *ptr = NULL;
674 char *cmd = NULL;
675 size_t cmdsize = 0;
676 FILE *dump;
677 bool persistent = TRUE;
678 bool sendfailure = FALSE;
679 size_t responsesize;
680 int error = 0;
681 char errbuf[STRERROR_LEN];
682 static char weare[256];
683 char responsedump[256];
685 snprintf(responsedump, sizeof(responsedump), "%s/%s", logdir, RESPONSE_DUMP);
687 logmsg("Send response number %ld part %ld", req->testno, req->partno);
689 switch(req->rcmd) {
690 default:
691 case RCMD_NORMALREQ:
692 break; /* continue with business as usual */
693 case RCMD_STREAM: {
694 static const char streamthis[] = "a string to stream 01234567890\n";
695 for(;;) {
696 written = swrite(sock, streamthis, CURL_CSTRLEN(streamthis));
697 if(got_exit_signal)
698 return -1;
699 if(written != (ssize_t)CURL_CSTRLEN(streamthis)) {
700 logmsg("Stopped streaming");
701 break;
702 }
703 }
704 return -1;
705 }
706 case RCMD_IDLE:
707 /* Do nothing. Sit idle. Pretend it rains. */
708 return 0;
709 }
711 req->open = FALSE;
713 if(req->testno < 0) {
714 size_t msglen;
715 char msgbuf[64];
717 switch(req->testno) {
718 case DOCNUMBER_QUIT:
719 logmsg("Replying to QUIT");
720 buffer = docquit_rtsp;
721 break;
722 case DOCNUMBER_WERULEZ:
723 /* we got a "friends?" question, reply back that we sure are */
724 logmsg("Identifying ourselves as friends");
725 snprintf(msgbuf, sizeof(msgbuf), "RTSP_SERVER WE ROOLZ: %ld\r\n",
726 (long)our_getpid());
727 msglen = strlen(msgbuf);
728 snprintf(weare, sizeof(weare),
729 "HTTP/1.1 200 OK\r\nContent-Length: %u\r\n\r\n%s",
730 (unsigned int)msglen, msgbuf);
731 buffer = weare;
732 break;
733 case DOCNUMBER_INTERNAL:
734 logmsg("Bailing out due to internal error");
735 return -1;
736 case DOCNUMBER_CONNECT:
737 logmsg("Replying to CONNECT");
738 buffer = docconnect;
739 break;
740 case DOCNUMBER_BADCONNECT:
741 logmsg("Replying to a bad CONNECT");
742 buffer = docbadconnect;
743 break;
744 case DOCNUMBER_404:
745 default:
746 logmsg("Replying with a 404");
747 if(req->protocol == RPROT_HTTP) {
748 buffer = doc404_HTTP;
749 }
750 else {
751 buffer = doc404_RTSP;
752 }
753 break;
754 }
756 count = strlen(buffer);
757 }
758 else {
759 FILE *stream = test2fopen(req->testno, logdir);
760 char partbuf[80] = "data";
761 if(req->partno)
762 snprintf(partbuf, sizeof(partbuf), "data%ld", req->partno);
763 if(!stream) {
764 error = errno;
765 logmsg("fopen() failed with error (%d) %s",
766 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
767 logmsg("Could not open test file");
768 return 0;
769 }
770 else {
771 error = getpart(&ptr, &count, "reply", partbuf, stream);
772 curlx_fclose(stream);
773 if(error) {
774 logmsg("getpart() failed with error (%d)", error);
775 return 0;
776 }
777 buffer = ptr;
778 }
780 if(got_exit_signal) {
781 free(ptr);
782 return -1;
783 }
785 /* re-open the same file again */
786 stream = test2fopen(req->testno, logdir);
787 if(!stream) {
788 error = errno;
789 logmsg("fopen() failed with error (%d) %s",
790 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
791 logmsg("Could not open test file");
792 free(ptr);
793 return 0;
794 }
795 else {
796 /* get the custom server control "commands" */
797 error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
798 curlx_fclose(stream);
799 if(error) {
800 logmsg("getpart() failed with error (%d)", error);
801 free(ptr);
802 return 0;
803 }
804 }
805 }
807 if(got_exit_signal) {
808 free(ptr);
809 free(cmd);
810 return -1;
811 }
813 /* If the word 'swsclose' is present anywhere in the reply chunk, the
814 connection is closed after the data has been sent to the requesting
815 client... */
816 if(strstr(buffer, "swsclose") || !count) {
817 persistent = FALSE;
818 logmsg("connection close instruction \"swsclose\" found in response");
819 }
820 if(strstr(buffer, "swsbounce")) {
821 rtspd_prevbounce = TRUE;
822 logmsg("enable \"swsbounce\" in the next request");
823 }
824 else
825 rtspd_prevbounce = FALSE;
827 dump = curlx_fopen(responsedump, "ab");
828 if(!dump) {
829 error = errno;
830 logmsg("fopen() failed with error (%d) %s",
831 error, curlx_strerror(error, errbuf, sizeof(errbuf)));
832 logmsg("Error opening file '%s'", responsedump);
833 logmsg("could not create logfile '%s'", responsedump);
834 free(ptr);
835 free(cmd);
836 return -1;
837 }
839 responsesize = count;
840 do {
841 /* Ok, we send no more than 200 bytes at a time, to make sure that
842 larger chunks are split up so that the client needs to do multiple
843 recv() calls to get it and thus we exercise that code better */
844 size_t num = count;
845 if(num > 200)
846 num = 200;
847 written = swrite(sock, buffer, num);
848 if(written < 0) {
849 sendfailure = TRUE;
850 break;
851 }
852 else {
853 logmsg("Sent off %ld bytes", (long)written);
854 }
855 /* write to file as well */
856 fwrite(buffer, 1, (size_t)written, dump);
857 if(got_exit_signal)
858 break;
860 count -= written;
861 buffer += written;
862 } while(count > 0);
864 /* Send out any RTP data */
865 if(req->rtp_buffer) {
866 logmsg("About to write %lu RTP bytes", (unsigned long)req->rtp_buffersize);
867 count = req->rtp_buffersize;
868 do {
869 size_t num = count;
870 if(num > 200)
871 num = 200;
872 written = swrite(sock, req->rtp_buffer + (req->rtp_buffersize - count),
873 num);
874 if(written < 0) {
875 sendfailure = TRUE;
876 break;
877 }
878 count -= written;
879 } while(count > 0);
881 free(req->rtp_buffer);
882 req->rtp_buffersize = 0;
883 }
885 if(curlx_fclose(dump))
886 logmsg("Error closing file %s error (%d) %s", responsedump,
887 errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));
889 if(got_exit_signal) {
890 free(ptr);
891 free(cmd);
892 return -1;
893 }
895 if(sendfailure) {
896 logmsg("Sending response failed. Only (%lu bytes) of "
897 "(%lu bytes) were sent",
898 (unsigned long)(responsesize - count),
899 (unsigned long)responsesize);
900 free(ptr);
901 free(cmd);
902 return -1;
903 }
905 logmsg("Response sent (%lu bytes) and written to %s",
906 (unsigned long)responsesize, responsedump);
907 free(ptr);
909 if(cmdsize > 0) {
910 char command[32];
911 int quarters;
912 int num;
913 ptr = cmd;
914 do {
915 if(sscanf(ptr, "%31s %d", command, &num) == 2) {
916 if(!strcmp("wait", command)) {
917 logmsg("Told to sleep for %d seconds", num);
918 quarters = num * 4;
919 while((quarters > 0) && !got_exit_signal) {
920 quarters--;
921 if(curlx_wait_ms(250)) {
922 /* should not happen */
923 int sockerr = SOCKERRNO;
924 logmsg("curlx_wait_ms() failed with error (%d) %s",
925 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
926 break;
927 }
928 }
929 if(!quarters)
930 logmsg("Continuing after sleeping %d seconds", num);
931 }
932 else
933 logmsg("Unknown command in reply command section");
934 }
935 ptr = strchr(ptr, '\n');
936 if(ptr)
937 ptr++;
938 else
939 ptr = NULL;
940 } while(ptr && *ptr);
941 }
942 free(cmd);
943 req->open = persistent;
945 rtspd_prevtestno = req->testno;
946 rtspd_prevpartno = req->partno;
948 return 0;
949}
951static int test_rtspd(int argc, const char *argv[])
952{
953 srvr_sockaddr_union_t me;
954 curl_socket_t sock = CURL_SOCKET_BAD;
955 curl_socket_t msgsock = CURL_SOCKET_BAD;
956 int wrotepidfile = 0;
957 int wroteportfile = 0;
958 int flag;
959 struct rtspd_httprequest req;
960 int rc;
961 int sockerr;
962 char errbuf[STRERROR_LEN];
963 int arg = 1;
965 memset(&req, 0, sizeof(req));
967 pidname = ".rtsp.pid";
968 serverlogfile = "log/rtspd.log";
969 serverlogslocked = 0;
970 server_port = 8999;
972 while(argc > arg) {
973 const char *opt;
974 curl_off_t num;
975 if(!strcmp("--version", argv[arg])) {
976 printf("rtspd IPv4%s"
977 "\n",
978#ifdef USE_IPV6
979 "/IPv6"
980#else
981 ""
982#endif
983 );
984 return 0;
985 }
986 else if(!strcmp("--pidfile", argv[arg])) {
987 arg++;
988 if(argc > arg)
989 pidname = argv[arg++];
990 }
991 else if(!strcmp("--portfile", argv[arg])) {
992 arg++;
993 if(argc > arg)
994 portname = argv[arg++];
995 }
996 else if(!strcmp("--logfile", argv[arg])) {
997 arg++;
998 if(argc > arg)
999 serverlogfile = argv[arg++];
1000 }
1001 else if(!strcmp("--logdir", argv[arg])) {
1002 arg++;
1003 if(argc > arg)
1004 logdir = argv[arg++];
1005 }
1006 else if(!strcmp("--ipv4", argv[arg])) {
1007 socket_type = "IPv4";
1008 socket_domain = AF_INET;
1009 arg++;
1010 }
1011 else if(!strcmp("--ipv6", argv[arg])) {
1012#ifdef USE_IPV6
1013 socket_type = "IPv6";
1014 socket_domain = AF_INET6;
1015#endif
1016 arg++;
1017 }
1018 else if(!strcmp("--port", argv[arg])) {
1019 arg++;
1020 if(argc > arg) {
1021 opt = argv[arg];
1022 if(!curlx_str_number(&opt, &num, 0xffff))
1023 server_port = (uint16_t)num;
1024 arg++;
1025 }
1026 }
1027 else if(!strcmp("--srcdir", argv[arg])) {
1028 arg++;
1029 if(argc > arg) {
1030 srcpath = argv[arg];
1031 arg++;
1032 }
1033 }
1034 else {
1035 puts("Usage: rtspd [option]\n"
1036 " --version\n"
1037 " --logfile [file]\n"
1038 " --logdir [directory]\n"
1039 " --pidfile [file]\n"
1040 " --portfile [file]\n"
1041 " --ipv4\n"
1042 " --ipv6\n"
1043 " --port [port]\n"
1044 " --srcdir [path]");
1045 return 0;
1046 }
1047 }
1049 snprintf(loglockfile, sizeof(loglockfile), "%s/%s/rtsp-%s.lock",
1050 logdir, SERVERLOGS_LOCKDIR, socket_type);
1052 install_signal_handlers(FALSE);
1054#ifdef USE_IPV6
1055 if(socket_domain == AF_INET6)
1056 sock = socket(AF_INET6, SOCK_STREAM, 0);
1057 else
1058#endif
1059 sock = socket(AF_INET, SOCK_STREAM, 0);
1061 if(sock == CURL_SOCKET_BAD) {
1062 sockerr = SOCKERRNO;
1063 logmsg("Error creating socket (%d) %s",
1064 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1065 goto server_cleanup;
1066 }
1068 flag = 1;
1069 if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&flag, sizeof(flag))) {
1070 sockerr = SOCKERRNO;
1071 logmsg("setsockopt(SO_REUSEADDR) failed with error (%d) %s",
1072 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1073 goto server_cleanup;
1074 }
1076#ifdef USE_IPV6
1077 if(socket_domain == AF_INET6) {
1078 memset(&me.sa6, 0, sizeof(me.sa6));
1079 me.sa6.sin6_family = AF_INET6;
1080 me.sa6.sin6_addr = in6addr_any;
1081 me.sa6.sin6_port = htons(server_port);
1082 rc = bind(sock, &me.sa, sizeof(me.sa6));
1083 }
1084 else
1085#endif
1086 {
1087 memset(&me.sa4, 0, sizeof(me.sa4));
1088 me.sa4.sin_family = AF_INET;
1089 me.sa4.sin_addr.s_addr = INADDR_ANY;
1090 me.sa4.sin_port = htons(server_port);
1091 rc = bind(sock, &me.sa, sizeof(me.sa4));
1092 }
1093 if(rc) {
1094 sockerr = SOCKERRNO;
1095 logmsg("Error binding socket on port %hu (%d) %s", server_port,
1096 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1097 goto server_cleanup;
1098 }
1100 if(!server_port) {
1101 /* The system was supposed to choose a port number, figure out which
1102 port we actually got and update the listener port value with it. */
1103 curl_socklen_t la_size;
1104 srvr_sockaddr_union_t localaddr;
1105 memset(&localaddr, 0, sizeof(localaddr));
1106#ifdef USE_IPV6
1107 if(socket_domain == AF_INET6)
1108 la_size = sizeof(localaddr.sa6);
1109 else
1110#endif
1111 la_size = sizeof(localaddr.sa4);
1112 if(getsockname(sock, &localaddr.sa, &la_size) < 0) {
1113 sockerr = SOCKERRNO;
1114 logmsg("getsockname() failed with error (%d) %s",
1115 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1116 sclose(sock);
1117 goto server_cleanup;
1118 }
1119 switch(localaddr.sa.sa_family) {
1120 case AF_INET:
1121 server_port = ntohs(localaddr.sa4.sin_port);
1122 break;
1123#ifdef USE_IPV6
1124 case AF_INET6:
1125 server_port = ntohs(localaddr.sa6.sin6_port);
1126 break;
1127#endif
1128 default:
1129 break;
1130 }
1131 if(!server_port) {
1132 /* Real failure, listener port shall not be zero beyond this point. */
1133 logmsg("Apparently getsockname() succeeded, with listener port zero.");
1134 logmsg("A valid reason for this failure is a binary built without");
1135 logmsg("proper network library linkage. This might not be the only");
1136 logmsg("reason, but double check it before anything else.");
1137 sclose(sock);
1138 goto server_cleanup;
1139 }
1140 }
1141 logmsg("Running %s version on port %d", socket_type, (int)server_port);
1143 /* start accepting connections */
1144 if(listen(sock, 5)) {
1145 sockerr = SOCKERRNO;
1146 logmsg("listen() failed with error (%d) %s",
1147 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1148 goto server_cleanup;
1149 }
1151 /*
1152 * As soon as this server writes its pid file the test harness attempts
1153 * to connect to this server and initiate its verification.
1154 */
1156 wrotepidfile = write_pidfile(pidname);
1157 if(!wrotepidfile)
1158 goto server_cleanup;
1160 if(portname) {
1161 wroteportfile = write_portfile(portname, server_port);
1162 if(!wroteportfile)
1163 goto server_cleanup;
1164 }
1166 for(;;) {
1167 msgsock = accept(sock, NULL, NULL);
1169 if(got_exit_signal)
1170 break;
1171 if(msgsock == CURL_SOCKET_BAD) {
1172 sockerr = SOCKERRNO;
1173 logmsg("MAJOR ERROR, accept() failed with error (%d) %s",
1174 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
1175 break;
1176 }
1178 /*
1179 * As soon as this server accepts a connection from the test harness it
1180 * must set the server logs advisor read lock to indicate that server
1181 * logs should not be read until this lock is removed by this server.
1182 */
1184 set_advisor_read_lock(loglockfile);
1185 serverlogslocked = 1;
1187 logmsg("====> Client connect");
1189#if defined(TCP_NODELAY) && !defined(__EMSCRIPTEN__)
1190 /*
1191 * Disable the Nagle algorithm to make it easier to send out a large
1192 * response in many small segments to torture the clients more.
1193 */
1194 flag = 1;
1195 if(setsockopt(msgsock, IPPROTO_TCP, TCP_NODELAY,
1196 (void *)&flag, sizeof(flag)) == -1) {
1197 logmsg("====> TCP_NODELAY failed");
1198 }
1199#endif
1201 /* initialization of httprequest struct is done in rtspd_get_request(),
1202 but due to pipelining treatment the pipelining struct field must be
1203 initialized previously to FALSE every time a new connection arrives. */
1205 req.pipelining = FALSE;
1207 do {
1208 if(got_exit_signal)
1209 break;
1211 if(rtspd_get_request(msgsock, &req))
1212 /* non-zero means error, break out of loop */
1213 break;
1215 if(rtspd_prevbounce) {
1216 /* bounce treatment requested */
1217 if(req.testno == rtspd_prevtestno) {
1218 req.partno = rtspd_prevpartno + 1;
1219 logmsg("BOUNCE part number to %ld", req.partno);
1220 }
1221 else {
1222 rtspd_prevbounce = FALSE;
1223 rtspd_prevtestno = -1;
1224 rtspd_prevpartno = -1;
1225 }
1226 }
1228 rtspd_send_doc(msgsock, &req);
1229 if(got_exit_signal)
1230 break;
1232 if((req.testno < 0) && (req.testno != DOCNUMBER_CONNECT)) {
1233 logmsg("special request received, no persistency");
1234 break;
1235 }
1236 if(!req.open) {
1237 logmsg("instructed to close connection after server-reply");
1238 break;
1239 }
1241 if(req.open)
1242 logmsg("=> persistent connection request ended, awaits new request");
1243 /* if we got a CONNECT, loop and get another request as well! */
1244 } while(req.open || (req.testno == DOCNUMBER_CONNECT));
1246 if(got_exit_signal)
1247 break;
1249 logmsg("====> Client disconnect");
1250 sclose(msgsock);
1251 msgsock = CURL_SOCKET_BAD;
1253 if(serverlogslocked) {
1254 serverlogslocked = 0;
1255 clear_advisor_read_lock(loglockfile);
1256 }
1258 if(req.testno == DOCNUMBER_QUIT)
1259 break;
1260 }
1262server_cleanup:
1264 if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
1265 sclose(msgsock);
1267 if(sock != CURL_SOCKET_BAD)
1268 sclose(sock);
1270 if(got_exit_signal)
1271 logmsg("signalled to die");
1273 if(wrotepidfile)
1274 unlink(pidname);
1275 if(wroteportfile)
1276 unlink(portname);
1278 if(serverlogslocked) {
1279 serverlogslocked = 0;
1280 clear_advisor_read_lock(loglockfile);
1281 }
1283 restore_signal_handlers(FALSE);
1285 return 0;
1286}
Diagnostics4 git calls · 144 ms
CallStatusSizeTime0144 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=138 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch20012 KiB138 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=12181 git.response.cache=MISS git.response.duration_ms=138 git.fetch.wants=12e6c6758e14342c383201d601c1ca2bc1944f3a git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=12181 git.pkt.payload.max=12160 git.sideband.band1.bytes=12159 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2dc981f02c125
- Request
- 2431c982…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format