Changes in uspace/app/websrv/websrv.c [b99f6e2:3e6a98c5] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/websrv/websrv.c
rb99f6e2 r3e6a98c5 44 44 #include <task.h> 45 45 46 #include < inet/addr.h>47 #include < inet/endpoint.h>48 #include < inet/tcp.h>46 #include <net/in.h> 47 #include <net/inet.h> 48 #include <net/socket.h> 49 49 50 50 #include <arg_parse.h> … … 56 56 57 57 #define DEFAULT_PORT 8080 58 #define BACKLOG_SIZE 3 58 59 59 60 #define WEB_ROOT "/data/web" … … 61 62 /** Buffer for receiving the request. */ 62 63 #define BUFFER_SIZE 1024 63 64 static void websrv_new_conn(tcp_listener_t *, tcp_conn_t *);65 66 static tcp_listen_cb_t listen_cb = {67 .new_conn = websrv_new_conn68 };69 70 static tcp_cb_t conn_cb = {71 .connected = NULL72 };73 64 74 65 static uint16_t port = DEFAULT_PORT; … … 131 122 132 123 /** Receive one character (with buffering) */ 133 static int recv_char(tcp_conn_t *conn, char *c) 134 { 135 size_t nrecv; 136 int rc; 137 124 static int recv_char(int fd, char *c) 125 { 138 126 if (rbuf_out == rbuf_in) { 139 127 rbuf_out = 0; 140 128 rbuf_in = 0; 141 129 142 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv);143 if (rc != EOK) {144 fprintf(stderr, " tcp_conn_recv() failed (%d)\n", rc);130 ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0); 131 if (rc <= 0) { 132 fprintf(stderr, "recv() failed (%zd)\n", rc); 145 133 return rc; 146 134 } 147 135 148 rbuf_in = nrecv;136 rbuf_in = rc; 149 137 } 150 138 … … 154 142 155 143 /** Receive one line with length limit */ 156 static int recv_line( tcp_conn_t *conn)144 static int recv_line(int fd) 157 145 { 158 146 char *bp = lbuf; … … 161 149 while (bp < lbuf + BUFFER_SIZE) { 162 150 char prev = c; 163 int rc = recv_char( conn, &c);151 int rc = recv_char(fd, &c); 164 152 165 153 if (rc != EOK) … … 199 187 } 200 188 201 static int send_response( tcp_conn_t *conn, const char *msg)189 static int send_response(int conn_sd, const char *msg) 202 190 { 203 191 size_t response_size = str_size(msg); … … 206 194 fprintf(stderr, "Sending response\n"); 207 195 208 int rc = tcp_conn_send(conn, (void *) msg, response_size);209 if (rc != EOK) {210 fprintf(stderr, " tcp_conn_send() failed\n");196 ssize_t rc = send(conn_sd, (void *) msg, response_size, 0); 197 if (rc < 0) { 198 fprintf(stderr, "send() failed\n"); 211 199 return rc; 212 200 } … … 215 203 } 216 204 217 static int uri_get(const char *uri, tcp_conn_t *conn)205 static int uri_get(const char *uri, int conn_sd) 218 206 { 219 207 if (str_cmp(uri, "/") == 0) … … 227 215 int fd = open(fname, O_RDONLY); 228 216 if (fd < 0) { 229 rc = send_response(conn , msg_not_found);217 rc = send_response(conn_sd, msg_not_found); 230 218 free(fname); 231 219 return rc; … … 234 222 free(fname); 235 223 236 rc = send_response(conn , msg_ok);224 rc = send_response(conn_sd, msg_ok); 237 225 if (rc != EOK) 238 226 return rc; … … 248 236 } 249 237 250 rc = tcp_conn_send(conn, fbuf, nr);251 if (rc != EOK) {252 fprintf(stderr, " tcp_conn_send() failed\n");238 rc = send(conn_sd, fbuf, nr, 0); 239 if (rc < 0) { 240 fprintf(stderr, "send() failed\n"); 253 241 close(fd); 254 242 return rc; … … 261 249 } 262 250 263 static int req_process( tcp_conn_t *conn)264 { 265 int rc = recv_line(conn );251 static int req_process(int conn_sd) 252 { 253 int rc = recv_line(conn_sd); 266 254 if (rc != EOK) { 267 255 fprintf(stderr, "recv_line() failed\n"); … … 273 261 274 262 if (str_lcmp(lbuf, "GET ", 4) != 0) { 275 rc = send_response(conn , msg_not_implemented);263 rc = send_response(conn_sd, msg_not_implemented); 276 264 return rc; 277 265 } … … 289 277 290 278 if (!uri_is_valid(uri)) { 291 rc = send_response(conn , msg_bad_request);292 return rc; 293 } 294 295 return uri_get(uri, conn );279 rc = send_response(conn_sd, msg_bad_request); 280 return rc; 281 } 282 283 return uri_get(uri, conn_sd); 296 284 } 297 285 … … 358 346 } 359 347 360 static void websrv_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)361 {362 int rc;363 364 if (verbose)365 fprintf(stderr, "New connection, waiting for request\n");366 367 rbuf_out = 0;368 rbuf_in = 0;369 370 rc = req_process(conn);371 if (rc != EOK) {372 fprintf(stderr, "Error processing request (%s)\n",373 str_error(rc));374 return;375 }376 377 rc = tcp_conn_send_fin(conn);378 if (rc != EOK) {379 fprintf(stderr, "Error sending FIN.\n");380 return;381 }382 }383 384 348 int main(int argc, char *argv[]) 385 349 { 386 inet_ep_t ep;387 tcp_listener_t *lst;388 tcp_t *tcp;389 int rc;390 391 350 /* Parse command line arguments */ 392 351 for (int i = 1; i < argc; i++) { 393 352 if (argv[i][0] == '-') { 394 rc = parse_option(argc, argv, &i);353 int rc = parse_option(argc, argv, &i); 395 354 if (rc != EOK) 396 355 return rc; … … 401 360 } 402 361 362 struct sockaddr_in addr; 363 364 addr.sin_family = AF_INET; 365 addr.sin_port = htons(port); 366 367 int rc = inet_pton(AF_INET, "127.0.0.1", (void *) 368 &addr.sin_addr.s_addr); 369 if (rc != EOK) { 370 fprintf(stderr, "Error parsing network address (%s)\n", 371 str_error(rc)); 372 return 1; 373 } 374 403 375 printf("%s: HelenOS web server\n", NAME); 404 376 405 377 if (verbose) 406 fprintf(stderr, "Creating listener\n"); 407 408 inet_ep_init(&ep); 409 ep.port = port; 410 411 rc = tcp_create(&tcp); 378 fprintf(stderr, "Creating socket\n"); 379 380 int listen_sd = socket(PF_INET, SOCK_STREAM, 0); 381 if (listen_sd < 0) { 382 fprintf(stderr, "Error creating listening socket (%s)\n", 383 str_error(listen_sd)); 384 return 2; 385 } 386 387 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr)); 412 388 if (rc != EOK) { 413 fprintf(stderr, "Error initializing TCP.\n");414 return 1;415 }416 417 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,418 &lst);389 fprintf(stderr, "Error binding socket (%s)\n", 390 str_error(rc)); 391 return 3; 392 } 393 394 rc = listen(listen_sd, BACKLOG_SIZE); 419 395 if (rc != EOK) { 420 fprintf(stderr, " Error creating listener.\n");421 return 2;396 fprintf(stderr, "listen() failed (%s)\n", str_error(rc)); 397 return 4; 422 398 } 423 399 … … 426 402 427 403 task_retval(0); 428 async_manager(); 404 405 while (true) { 406 struct sockaddr_in raddr; 407 socklen_t raddr_len = sizeof(raddr); 408 int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr, 409 &raddr_len); 410 411 if (conn_sd < 0) { 412 fprintf(stderr, "accept() failed (%s)\n", str_error(rc)); 413 continue; 414 } 415 416 if (verbose) { 417 fprintf(stderr, "Connection accepted (sd=%d), " 418 "waiting for request\n", conn_sd); 419 } 420 421 rbuf_out = 0; 422 rbuf_in = 0; 423 424 rc = req_process(conn_sd); 425 if (rc != EOK) 426 fprintf(stderr, "Error processing request (%s)\n", 427 str_error(rc)); 428 429 rc = closesocket(conn_sd); 430 if (rc != EOK) { 431 fprintf(stderr, "Error closing connection socket (%s)\n", 432 str_error(rc)); 433 closesocket(listen_sd); 434 return 5; 435 } 436 437 if (verbose) 438 fprintf(stderr, "Connection closed\n"); 439 } 429 440 430 441 /* Not reached */
Note:
See TracChangeset
for help on using the changeset viewer.