Skip to content

Commit

Permalink
Integrate linenoise package with web server
Browse files Browse the repository at this point in the history
In the previous version of console implementation, we tried to integrate
tiny-web-server to enable the ability of processing commands from web
requests. As the result, the package linenoise which is responsible for
command-line auto-complete needs to be disabled during the running time
of tiny-web-server. Because the `line_edit()` function in linenoise.c
doesn't have the ability to handle web requests correctly.

When we start the web server, we use `cmd_select` in console.c and use
`select` system call to monitor web socket file descriptor and stdin_fd
at the same time, however, this ability should present in the
`line_edit` function in linenoise.c so we can process commands from
command-line and from web requests at the same time.

When we start the web server, we use `cmd_select` in console.c and use
`select` function to monitor web socket file descriptor and stdin_fd at
the same time. I re-design the control flow of web request and
command-line input by implement the hook function `web_select()`,
which use the function `select()` to monitor both input file descriptor
 and web file descriptor and modify `line_edit()` to use `web_select()`
so we can process command-line input as normal.

One may wonder why don't we simply modify the function `line_edt()`, the
reason is that linenoise is an upstream package so we only want to do
the miminal changes to this package.

Change change change

change change change
  • Loading branch information
vax-r committed Mar 1, 2024
1 parent 267cca7 commit 7030a6d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 43 deletions.
51 changes: 9 additions & 42 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,13 @@ static int cmd_select(int nfds,
fd_set *exceptfds,
struct timeval *timeout)
{
int infd;
fd_set local_readset;

if (cmd_done())
return 0;

if (!block_flag) {
int infd;
/* Process any commands in input buffer */
if (!readfds)
readfds = &local_readset;
Expand All @@ -581,51 +581,18 @@ static int cmd_select(int nfds,
FD_SET(web_fd, readfds);

if (infd == STDIN_FILENO && prompt_flag) {
printf("%s", prompt);
char *cmdline = linenoise(prompt);
if (cmdline)
interpret_cmd(cmdline);
fflush(stdout);
prompt_flag = true;
} else if (infd != STDIN_FILENO) {
char *cmdline = readline();
if (cmdline)
interpret_cmd(cmdline);
}

if (infd >= nfds)
nfds = infd + 1;
if (web_fd >= nfds)
nfds = web_fd + 1;
}
if (nfds == 0)
return 0;

int result = select(nfds, readfds, writefds, exceptfds, timeout);
if (result <= 0)
return result;

infd = buf_stack->fd;
if (readfds && FD_ISSET(infd, readfds)) {
/* Commandline input available */
FD_CLR(infd, readfds);
result--;

set_echo(0);
char *cmdline = readline();
if (cmdline)
interpret_cmd(cmdline);
} else if (readfds && FD_ISSET(web_fd, readfds)) {
FD_CLR(web_fd, readfds);
result--;
struct sockaddr_in clientaddr;
socklen_t clientlen = sizeof(clientaddr);
web_connfd =
accept(web_fd, (struct sockaddr *) &clientaddr, &clientlen);

char *p = web_recv(web_connfd, &clientaddr);
char *buffer = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n";
web_send(web_connfd, buffer);

if (p)
interpret_cmd(p);
free(p);
close(web_connfd);
}
return result;
return 0;
}

bool finish_cmd()
Expand Down
8 changes: 7 additions & 1 deletion linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
#include <unistd.h>

#include "linenoise.h"
#include "web.h"

#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
#define LINENOISE_MAX_LINE 4096
Expand Down Expand Up @@ -927,6 +928,11 @@ static int line_edit(int stdin_fd,

if (write(l.ofd, prompt, l.plen) == -1)
return -1;

int result = web_select(l.buf, l.ifd);
if (result != 0)
return result;

while (1) {
signed char c;
int nread;
Expand Down Expand Up @@ -1362,4 +1368,4 @@ int line_history_load(const char *filename)
}
fclose(fp);
return 0;
}
}
37 changes: 37 additions & 0 deletions web.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#define TCP_CORK TCP_NOPUSH
#endif

int server_fd;

typedef struct {
int fd; /* descriptor for this buf */
int count; /* unread byte in this buf */
Expand Down Expand Up @@ -153,6 +155,9 @@ int web_open(int port)
/* Make it a listening socket ready to accept connection requests */
if (listen(listenfd, LISTENQ) < 0)
return -1;

server_fd = listenfd;

return listenfd;
}

Expand Down Expand Up @@ -228,3 +233,35 @@ char *web_recv(int fd, struct sockaddr_in *clientaddr)

return ret;
}

int web_select(char *buf, int stdin_fd)
{
fd_set listenset;

FD_ZERO(&listenset);
FD_SET(stdin_fd, &listenset);
int max_fd = stdin_fd;
if (server_fd > 0) {
FD_SET(server_fd, &listenset);
max_fd = max_fd > server_fd ? max_fd : server_fd;
}
int result = select(max_fd + 1, &listenset, NULL, NULL, NULL);
if (result < 0)
return -1;

if (server_fd > 0 && FD_ISSET(server_fd, &listenset)) {
FD_CLR(server_fd, &listenset);
struct sockaddr_in clientaddr;
socklen_t clientlen = sizeof(clientaddr);
int web_connfd =
accept(server_fd, (struct sockaddr *) &clientaddr, &clientlen);

char *p = web_recv(web_connfd, &clientaddr);
char *buffer = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n";
web_send(web_connfd, buffer);
strncpy(buf, p, strlen(p) + 1);
free(p);
close(web_connfd);
}
return strlen(buf);
}
2 changes: 2 additions & 0 deletions web.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ char *web_recv(int fd, struct sockaddr_in *clientaddr);

void web_send(int out_fd, char *buffer);

int web_select(char *buf, int stdin_fd);

#endif

0 comments on commit 7030a6d

Please sign in to comment.