Skip to content

Commit

Permalink
Successfully integrate linenoise 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.

That's the reason I re-design the linenoise implementation and make some
modification to put `select()` system call in `line_edit` so we can have
the full ability to use web server and linenoise package in command-line
at the same time.
  • Loading branch information
vax-r committed Mar 1, 2024
1 parent 267cca7 commit 39cfd90
Show file tree
Hide file tree
Showing 4 changed files with 60 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
38 changes: 37 additions & 1 deletion linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,20 @@

#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#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 @@ -180,6 +183,7 @@ static void line_atexit(void);
int line_history_add(const char *line);
static void refresh_line(struct line_state *l);


/* Debugging macro. */
#if 0
FILE *lndebug_fp = NULL;
Expand Down Expand Up @@ -927,6 +931,37 @@ static int line_edit(int stdin_fd,

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

fd_set set;

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

if (web_fd > 0 && FD_ISSET(web_fd, &set)) {
FD_CLR(web_fd, &set);
struct sockaddr_in clientaddr;
socklen_t clientlen = sizeof(clientaddr);
int 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);
strncpy(buf, p, strlen(p) + 1);
free(p);
close(web_connfd);
return strlen(buf);
}

while (1) {
signed char c;
int nread;
Expand Down Expand Up @@ -1215,6 +1250,7 @@ char *linenoise(const char *prompt)
return strdup(buf);
}


/* This is just a wrapper the user may want to call in order to make sure
* the linenoise returned buffer is freed with the same allocator it was
* created with. Useful when the main program is using an alternative
Expand Down Expand Up @@ -1362,4 +1398,4 @@ int line_history_load(const char *filename)
}
fclose(fp);
return 0;
}
}
12 changes: 12 additions & 0 deletions web.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#define TCP_CORK TCP_NOPUSH
#endif

int server_fd;
uint8_t serving = 0;

typedef struct {
int fd; /* descriptor for this buf */
int count; /* unread byte in this buf */
Expand Down Expand Up @@ -153,6 +156,10 @@ 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;
serving = 1;

return listenfd;
}

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

return ret;
}

int web_getfd()
{
return serving ? server_fd : -1;
}
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_getfd();

#endif

0 comments on commit 39cfd90

Please sign in to comment.