Skip to content

Commit

Permalink
Integrate Tiny Web Server
Browse files Browse the repository at this point in the history
In the previous version, to avoid conflicts between stdin and web
requests, stdin was closed after executing do_web. Here, a solution
is implemented by adding a select mechanism in the line_edit function
to handle stdin and socketfd simultaneously, resolving the conflict.
  • Loading branch information
HotMercury committed Mar 13, 2024
1 parent f237c7d commit 48effe8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
22 changes: 12 additions & 10 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static bool interpret_cmda(int argc, char *argv[])

return ok;
}

int web_connfd;
/* Execute a command from a command line */
static bool interpret_cmd(char *cmdline)
{
Expand All @@ -203,6 +203,9 @@ static bool interpret_cmd(char *cmdline)
free_string(argv[i]);
free_array(argv, argc, sizeof(char *));

if (web_connfd) {
close(web_connfd);
}
return ok;
}

Expand Down Expand Up @@ -392,7 +395,7 @@ static bool do_time(int argc, char *argv[])
}

static bool use_linenoise = true;
static int web_fd;
int web_fd = -1;

static bool do_web(int argc, char *argv[])
{
Expand All @@ -403,9 +406,11 @@ static bool do_web(int argc, char *argv[])
}

web_fd = web_open(port);
int flag = fcntl(web_fd, F_GETFL, 0);
fcntl(web_fd, F_SETFL, flag | O_NONBLOCK);
if (web_fd > 0) {
printf("listen on port %d, fd is %d\n", port, web_fd);
use_linenoise = false;
// use_linenoise = false;
} else {
perror("ERROR");
exit(web_fd);
Expand Down Expand Up @@ -553,13 +558,14 @@ static bool cmd_done()
* nfds should be set to the maximum file descriptor for network sockets.
* If nfds == 0, this indicates that there is no pending network activity
*/
int web_connfd;

static int cmd_select(int nfds,
fd_set *readfds,
fd_set *writefds,
fd_set *exceptfds,
struct timeval *timeout)
{
printf("cmd_select\n");
int infd;
fd_set local_readset;

Expand Down Expand Up @@ -684,22 +690,18 @@ bool run_console(char *infile_name)
report(1, "ERROR: Could not open source file '%s'", infile_name);
return false;
}

if (!has_infile) {
char *cmdline;
while (use_linenoise && (cmdline = linenoise(prompt))) {
interpret_cmd(cmdline);
line_history_add(cmdline); /* Add to the history. */
line_history_save(HISTORY_FILE); /* Save the history on disk. */
line_free(cmdline);
while (buf_stack && buf_stack->fd != STDIN_FILENO)
while (buf_stack && buf_stack->fd != STDIN_FILENO) {
cmd_select(0, NULL, NULL, NULL, NULL);
}
has_infile = false;
}
if (!use_linenoise) {
while (!cmd_done())
cmd_select(0, NULL, NULL, NULL, NULL);
}
} else {
while (!cmd_done())
cmd_select(0, NULL, NULL, NULL, NULL);
Expand Down
60 changes: 59 additions & 1 deletion linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,14 @@ static void line_edit_next_word(struct line_state *l)
*
* The function returns the length of the current buffer.
*/
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "linenoise.h"
#include "web.h"
extern int web_fd;
extern int web_connfd;
static int line_edit(int stdin_fd,
int stdout_fd,
char *buf,
Expand Down Expand Up @@ -924,14 +932,64 @@ static int line_edit(int stdin_fd,
* initially is just an empty string.
*/
line_history_add("");

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

fd_set set;
FD_ZERO(&set);
int nfd = l.ifd + 1;
if (web_fd != -1) {
FD_SET(web_fd, &set); // set socket
nfd = web_fd + 1;
}
FD_SET(l.ifd, &set); // set stdin


while (1) {
signed char c;
int nread;
char seq[5];

int rv = select(nfd, &set, NULL, NULL, NULL);
struct sockaddr_in clientaddr;
socklen_t client_len = sizeof(clientaddr);

switch (rv) {
case -1:
perror("select");
break;
case 0:
printf("timeout\n");
continue;
default:
if (web_fd != -1 && FD_ISSET(web_fd, &set)) {
FD_CLR(web_fd, &set); // auto clear?
int connfd;
connfd = accept(web_fd, (struct sockaddr *) &clientaddr,
&client_len);
char *p = web_recv(connfd, &clientaddr);
char *buffer =
"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n";
web_send(connfd, buffer);

// web_send(web_connfd, "fuck");
int len = 0;
if (p) {
strncpy(buf, p, strlen(p) + 1);
len = strlen(p);
}
web_connfd = connfd;
free(p);
return len;
}
// else if(FD_ISSET(l.ifd, &set)){
// nread = read(l.ifd, &c, 1);
// if (nread <= 0)
// return l.len;
// }
break;
}

nread = read(l.ifd, &c, 1);
if (nread <= 0)
return l.len;
Expand Down

0 comments on commit 48effe8

Please sign in to comment.