Skip to content

Commit 21fc9de

Browse files
committed
HTTP keep-alive fixed. Network debugging capabilities.
1 parent e40e55b commit 21fc9de

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -208,27 +208,31 @@ before the first peak to provide some context.
208208

209209
It is possible to display different categories of messages:
210210

211-
--debug 1 Displays all the messages correctly demoudulated.
211+
--debug 1 Displays all the messages correctly demoudulated.
212212
A correctly demodulated message is just one that
213213
makes sense as a Mode S message, the preamble makes
214214
sense, and there are no message errors, that is,
215215
no adiacet samples describing bits are the same
216216
magnitude.
217217

218-
--debug 2 Only messages with demodulation errors are displayed,
218+
--debug 2 Only messages with demodulation errors are displayed,
219219
That is, only messages where one or more adiacent
220220
samples that should describe bits are the same
221221
magnitude.
222222

223-
--debug 3 Correctly deooded messages with Bad CRC are displayed.
223+
--debug 3 Correctly deooded messages with Bad CRC are displayed.
224224

225-
--debug 4 Correctly deooded messages with good CRC are displayed.
225+
--debug 4 Correctly deooded messages with good CRC are displayed.
226226

227-
--debug 5 Preamble detection failed in some way (specified when
227+
--debug 5 Preamble detection failed in some way (specified when
228228
dumping the samples) even if the current sample level
229229
is greater than MODES_DEBUG_NOPREAMBLE_LEVEL (set to
230230
25 by default).
231231

232+
Network related debug modes:
233+
234+
--debug 6 Log network events (HTTP requests & others)
235+
232236
How this program works?
233237
---
234238

anet.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ int anetNonBlock(char *err, int fd);
5454
int anetTcpNoDelay(char *err, int fd);
5555
int anetTcpKeepAlive(char *err, int fd);
5656
int anetPeerToString(int fd, char *ip, int *port);
57+
int anetSetSendBuffer(char *err, int fd, int buffsize);
5758

5859
#endif

dump1090.c

+31-5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#define MODES_DEBUG_BADCRC 3
7171
#define MODES_DEBUG_GOODCRC 4
7272
#define MODES_DEBUG_NOPREAMBLE 5
73+
#define MODES_DEBUG_NET 6
7374

7475
/* When debug is set to MODES_DEBUG_NOPREAMBLE, the first sample must be
7576
* at least greater than a given level for us to dump the signal. */
@@ -84,6 +85,7 @@
8485
#define MODES_NET_INPUT_RAW_PORT 30001
8586
#define MODES_NET_HTTP_PORT 8080
8687
#define MODES_CLIENT_BUF_SIZE 1024
88+
#define MODES_NET_SNDBUF_SIZE (1024*64)
8789

8890
#define MODES_NOTUSED(V) ((void) V)
8991

@@ -1734,9 +1736,13 @@ void modesAcceptClients(void) {
17341736
c->fd = fd;
17351737
c->buflen = 0;
17361738
Modes.clients[fd] = c;
1739+
anetSetSendBuffer(Modes.aneterr,fd,MODES_NET_SNDBUF_SIZE);
17371740

17381741
if (Modes.maxfd < fd) Modes.maxfd = fd;
17391742
j--; /* Try again with the same listening port. */
1743+
1744+
if (Modes.debug == MODES_DEBUG_NET)
1745+
printf("Created new client %d\n", fd);
17401746
}
17411747
}
17421748

@@ -1746,6 +1752,9 @@ void modesFreeClient(int fd) {
17461752
free(Modes.clients[fd]);
17471753
Modes.clients[fd] = NULL;
17481754

1755+
if (Modes.debug == MODES_DEBUG_NET)
1756+
printf("Closing client %d\n", fd);
1757+
17491758
/* If this was our maxfd, rescan the full clients array to check what's
17501759
* the new max. */
17511760
if (Modes.maxfd == fd) {
@@ -1901,21 +1910,35 @@ char *aircraftsToJson(int *len) {
19011910
int handleHTTPRequest(struct client *c) {
19021911
char hdr[512];
19031912
int clen, hdrlen;
1904-
int keepalive;
1913+
int httpver, keepalive;
19051914
char *p, *url, *content;
19061915
char *ctype;
19071916

1908-
/* printf("HTTP request: %s\n", c->buf); */
1917+
if (Modes.debug == MODES_DEBUG_NET)
1918+
printf("\nHTTP request: %s\n", c->buf);
19091919

19101920
/* Minimally parse the request. */
1911-
keepalive = strstr(c->buf, "keep-alive") != NULL;
1921+
httpver = (strstr(c->buf, "HTTP/1.1") != NULL) ? 11 : 10;
1922+
if (httpver == 10) {
1923+
/* HTTP 1.0 defaults to close, unless otherwise specified. */
1924+
keepalive = strstr(c->buf, "Connection: keep-alive") != NULL;
1925+
} else if (httpver == 11) {
1926+
/* HTTP 1.1 defaults to keep-alive, unless close is specified. */
1927+
keepalive = strstr(c->buf, "Connection: close") == NULL;
1928+
}
1929+
1930+
/* Identify he URL. */
19121931
p = strchr(c->buf,' ');
19131932
if (!p) return 1; /* There should be the method and a space... */
19141933
url = ++p; /* Now this should point to the requested URL. */
19151934
p = strchr(p, ' ');
19161935
if (!p) return 1; /* There should be a space before HTTP/... */
19171936
*p = '\0';
1918-
/* printf("URL: %s\n", url); */
1937+
1938+
if (Modes.debug == MODES_DEBUG_NET) {
1939+
printf("\nHTTP keep alive: %d\n", keepalive);
1940+
printf("HTTP requested URL: %s\n\n", url);
1941+
}
19191942

19201943
/* Select the content to send, we have just two so far:
19211944
* "/" -> Our google map application.
@@ -1959,6 +1982,9 @@ int handleHTTPRequest(struct client *c) {
19591982
keepalive ? "keep-alive" : "close",
19601983
clen);
19611984

1985+
if (Modes.debug == MODES_DEBUG_NET)
1986+
printf("HTTP Reply header:\n%s", hdr);
1987+
19621988
/* Send header and content. */
19631989
if (write(c->fd, hdr, hdrlen) == -1 ||
19641990
write(c->fd, content, clen) == -1)
@@ -1968,7 +1994,7 @@ int handleHTTPRequest(struct client *c) {
19681994
}
19691995
free(content);
19701996
Modes.stat_http_requests++;
1971-
return 0;
1997+
return !keepalive;
19721998
}
19731999

19742000
/* This function polls the clients using read() in order to receive new

0 commit comments

Comments
 (0)