SpriteHTTPD - embedded HTTP server with read-only filesystem and templating, originally developed for ESP8266, now stand-alone and POSIX compatible.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
spritehttpd/lib/src/httpd-loop.c

235 lines
8.4 KiB

/*
ESP8266 web server - platform-dependent routines, FreeRTOS version
Thanks to my collague at Espressif for writing the foundations of this code.
*/
#include "httpd.h"
#include "platform.h"
#include "httpd-platform.h"
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <netinet/tcp.h>
#include "logging.h"
static int httpPort;
static int httpMaxConnCt;
struct HttpdConnType {
int fd;
int needWriteDoneNotif;
int needsClose;
int port;
char ip[4];
};
static HttpdConnType s_rconn[HTTPD_MAX_CONNECTIONS];
int httpdConnSendData(ConnTypePtr conn, char *buff, int len)
{
conn->needWriteDoneNotif = 1;
return (write(conn->fd, buff, len) >= 0);
}
void httpdConnDisconnect(ConnTypePtr conn)
{
conn->needsClose = 1;
conn->needWriteDoneNotif = 1; //because the real close is done in the writable select code
}
#define RECV_BUF_SIZE 2048
void platHttpServerTask(void *pvParameters)
{
int32_t listenfd;
int32_t remotefd;
int32_t len;
int32_t ret;
int x;
int maxfdp = 0;
char *precvbuf;
fd_set readset, writeset;
struct sockaddr name;
//struct timeval timeout;
struct sockaddr_in server_addr;
struct sockaddr_in remote_addr;
struct httpd_options *options = pvParameters;
if (options == NULL) {
httpPort = 80;
} else {
httpPort = options->port;
}
for (x = 0; x < HTTPD_MAX_CONNECTIONS; x++) {
s_rconn[x].fd = -1;
}
/* Construct local address structure */
memset(&server_addr, 0, sizeof(server_addr)); /* Zero out structure */
server_addr.sin_family = AF_INET; /* Internet address family */
server_addr.sin_addr.s_addr = INADDR_ANY; /* Any incoming interface */
//server_addr.sin_len = sizeof(server_addr);
server_addr.sin_port = htons(httpPort); /* Local port */
/* Create socket for incoming connections */
do {
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1) {
error("platHttpServerTask: failed to create sock!");
httpdPlatDelayMs(1000);
}
} while (listenfd == -1);
/* Bind to the local port */
do {
ret = bind(listenfd, (struct sockaddr *) &server_addr, sizeof(server_addr));
if (ret != 0) {
error("platHttpServerTask: failed to bind!");
httpdPlatDelayMs(1000);
}
} while (ret != 0);
do {
/* Listen to the local connection */
ret = listen(listenfd, HTTPD_MAX_CONNECTIONS);
if (ret != 0) {
error("platHttpServerTask: failed to listen!");
httpdPlatDelayMs(1000);
}
} while (ret != 0);
info("esphttpd: active and listening to connections.");
while (1) {
// clear fdset, and set the select function wait time
int socketsFull = 1;
maxfdp = 0;
FD_ZERO(&readset);
FD_ZERO(&writeset);
//timeout.tv_sec = 2;
//timeout.tv_usec = 0;
for (x = 0; x < HTTPD_MAX_CONNECTIONS; x++) {
if (s_rconn[x].fd != -1) {
FD_SET(s_rconn[x].fd, &readset);
if (s_rconn[x].needWriteDoneNotif) FD_SET(s_rconn[x].fd, &writeset);
if (s_rconn[x].fd > maxfdp) { maxfdp = s_rconn[x].fd; }
} else {
socketsFull = 0;
}
}
if (!socketsFull) {
FD_SET(listenfd, &readset);
if (listenfd > maxfdp) { maxfdp = listenfd; }
}
//polling all exist client handle,wait until readable/writable
ret = select(maxfdp + 1, &readset, &writeset, NULL, NULL);//&timeout
if (ret > 0) {
//See if we need to accept a new connection
if (FD_ISSET(listenfd, &readset)) {
len = sizeof(struct sockaddr_in);
remotefd = accept(listenfd, (struct sockaddr *) &remote_addr, (socklen_t *) &len);
if (remotefd < 0) {
warn("platHttpServerTask: Huh? Accept failed.");
continue;
}
for (x = 0; x < HTTPD_MAX_CONNECTIONS; x++) { if (s_rconn[x].fd == -1) { break; }}
if (x == HTTPD_MAX_CONNECTIONS) {
warn("platHttpServerTask: Huh? Got accept with all slots full.");
continue;
}
int keepAlive = 1; //enable keepalive
int keepIdle = 60; //60s
int keepInterval = 5; //5s
int keepCount = 3; //retry times
setsockopt(remotefd, SOL_SOCKET, SO_KEEPALIVE, (void *) &keepAlive, sizeof(keepAlive));
setsockopt(remotefd, IPPROTO_TCP, TCP_KEEPIDLE, (void *) &keepIdle, sizeof(keepIdle));
setsockopt(remotefd, IPPROTO_TCP, TCP_KEEPINTVL, (void *) &keepInterval, sizeof(keepInterval));
setsockopt(remotefd, IPPROTO_TCP, TCP_KEEPCNT, (void *) &keepCount, sizeof(keepCount));
s_rconn[x].fd = remotefd;
s_rconn[x].needWriteDoneNotif = 0;
s_rconn[x].needsClose = 0;
len = sizeof(name);
getpeername(remotefd, &name, (socklen_t *) &len);
struct sockaddr_in *piname = (struct sockaddr_in *) &name;
s_rconn[x].port = piname->sin_port;
memcpy(&s_rconn[x].ip, &piname->sin_addr.s_addr, sizeof(s_rconn[x].ip));
httpdConnectCb(&s_rconn[x], s_rconn[x].ip, s_rconn[x].port);
//os_timer_disarm(&connData[x].conn->stop_watch);
//os_timer_setfn(&connData[x].conn->stop_watch, (os_timer_func_t *)httpserver_conn_watcher, connData[x].conn);
//os_timer_arm(&connData[x].conn->stop_watch, STOP_TIMER, 0);
// dbg("httpserver acpt index %d sockfd %d!", x, remotefd);
}
//See if anything happened on the existing connections.
for (x = 0; x < HTTPD_MAX_CONNECTIONS; x++) {
//Skip empty slots
if (s_rconn[x].fd == -1) { continue; }
//Check for write availability first: the read routines may write needWriteDoneNotif while
//the select didn't check for that.
if (s_rconn[x].needWriteDoneNotif && FD_ISSET(s_rconn[x].fd, &writeset)) {
s_rconn[x].needWriteDoneNotif = 0; //Do this first, httpdSentCb may write something making this 1 again.
if (s_rconn[x].needsClose) {
//Do callback and close fd.
httpdDisconCb(&s_rconn[x], s_rconn[x].ip, s_rconn[x].port);
close(s_rconn[x].fd);
s_rconn[x].fd = -1;
} else {
httpdSentCb(&s_rconn[x], s_rconn[x].ip, s_rconn[x].port);
}
}
if (FD_ISSET(s_rconn[x].fd, &readset)) {
precvbuf = (char *) malloc(RECV_BUF_SIZE);
if (precvbuf == NULL) {
error("platHttpServerTask: memory exhausted!");
httpdDisconCb(&s_rconn[x], s_rconn[x].ip, s_rconn[x].port);
close(s_rconn[x].fd);
s_rconn[x].fd = -1;
}
ret = (int) recv(s_rconn[x].fd, precvbuf, RECV_BUF_SIZE, 0);
if (ret > 0) {
//Data received. Pass to httpd.
httpdRecvCb(&s_rconn[x], s_rconn[x].ip, s_rconn[x].port, precvbuf, ret);
} else {
//recv error,connection close
httpdDisconCb(&s_rconn[x], s_rconn[x].ip, s_rconn[x].port);
close(s_rconn[x].fd);
s_rconn[x].fd = -1;
}
if (precvbuf) { free(precvbuf); }
}
}
}
}
//Deinit code, not used here.
/*release data connection*/
for (x = 0; x < HTTPD_MAX_CONNECTIONS; x++) {
//find all valid handle
if (s_connData[x]->conn == NULL) { continue; }
if (s_connData[x]->conn->fd >= 0) {
//os_timer_disarm((os_timer_t *)&connData[x].conn->stop_watch); // ???
close(s_connData[x]->conn->fd);
s_connData[x]->conn->fd = -1;
s_connData[x]->conn = NULL;
if (s_connData[x]->cgi != NULL) { s_connData[x]->cgi(s_connData[x]); } //flush cgi data
}
}
/*release listen socket*/
close(listenfd);
httpdPlatTaskEnd();
}