Changeset fab2746 in mainline for uspace/app/websrv/websrv.c
- Timestamp:
- 2015-04-08T21:25:30Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 99ea91b2
- Parents:
- ba0eac5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/websrv/websrv.c
rba0eac5 rfab2746 44 44 #include <task.h> 45 45 46 #include < net/in.h>47 #include < net/inet.h>48 #include < net/socket.h>46 #include <inet/addr.h> 47 #include <inet/endpoint.h> 48 #include <inet/tcp.h> 49 49 50 50 #include <arg_parse.h> … … 56 56 57 57 #define DEFAULT_PORT 8080 58 #define BACKLOG_SIZE 359 58 60 59 #define WEB_ROOT "/data/web" … … 62 61 /** Buffer for receiving the request. */ 63 62 #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_conn 68 }; 69 70 static tcp_cb_t conn_cb = { 71 .connected = NULL 72 }; 64 73 65 74 static uint16_t port = DEFAULT_PORT; … … 122 131 123 132 /** Receive one character (with buffering) */ 124 static int recv_char(int fd, char *c) 125 { 133 static int recv_char(tcp_conn_t *conn, char *c) 134 { 135 size_t nrecv; 136 int rc; 137 126 138 if (rbuf_out == rbuf_in) { 127 139 rbuf_out = 0; 128 140 rbuf_in = 0; 129 141 130 ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);131 if (rc <= 0) {132 fprintf(stderr, "recv() failed (% zd)\n", rc);142 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv); 143 if (rc != EOK) { 144 fprintf(stderr, "recv() failed (%d)\n", rc); 133 145 return rc; 134 146 } 135 147 136 rbuf_in = rc;148 rbuf_in = nrecv; 137 149 } 138 150 … … 142 154 143 155 /** Receive one line with length limit */ 144 static int recv_line( int fd)156 static int recv_line(tcp_conn_t *conn) 145 157 { 146 158 char *bp = lbuf; … … 149 161 while (bp < lbuf + BUFFER_SIZE) { 150 162 char prev = c; 151 int rc = recv_char( fd, &c);163 int rc = recv_char(conn, &c); 152 164 153 165 if (rc != EOK) … … 187 199 } 188 200 189 static int send_response( int conn_sd, const char *msg)201 static int send_response(tcp_conn_t *conn, const char *msg) 190 202 { 191 203 size_t response_size = str_size(msg); … … 194 206 fprintf(stderr, "Sending response\n"); 195 207 196 ssize_t rc = send(conn_sd, (void *) msg, response_size, 0);197 if (rc < 0) {198 fprintf(stderr, " send() failed\n");208 int rc = tcp_conn_send(conn, (void *) msg, response_size); 209 if (rc != EOK) { 210 fprintf(stderr, "tcp_conn_send() failed\n"); 199 211 return rc; 200 212 } … … 203 215 } 204 216 205 static int uri_get(const char *uri, int conn_sd)217 static int uri_get(const char *uri, tcp_conn_t *conn) 206 218 { 207 219 if (str_cmp(uri, "/") == 0) … … 215 227 int fd = open(fname, O_RDONLY); 216 228 if (fd < 0) { 217 rc = send_response(conn _sd, msg_not_found);229 rc = send_response(conn, msg_not_found); 218 230 free(fname); 219 231 return rc; … … 222 234 free(fname); 223 235 224 rc = send_response(conn _sd, msg_ok);236 rc = send_response(conn, msg_ok); 225 237 if (rc != EOK) 226 238 return rc; … … 236 248 } 237 249 238 rc = send(conn_sd, fbuf, nr, 0);239 if (rc < 0) {240 fprintf(stderr, " send() failed\n");250 rc = tcp_conn_send(conn, fbuf, nr); 251 if (rc != EOK) { 252 fprintf(stderr, "tcp_conn_send() failed\n"); 241 253 close(fd); 242 254 return rc; … … 249 261 } 250 262 251 static int req_process( int conn_sd)252 { 253 int rc = recv_line(conn _sd);263 static int req_process(tcp_conn_t *conn) 264 { 265 int rc = recv_line(conn); 254 266 if (rc != EOK) { 255 267 fprintf(stderr, "recv_line() failed\n"); … … 261 273 262 274 if (str_lcmp(lbuf, "GET ", 4) != 0) { 263 rc = send_response(conn _sd, msg_not_implemented);275 rc = send_response(conn, msg_not_implemented); 264 276 return rc; 265 277 } … … 277 289 278 290 if (!uri_is_valid(uri)) { 279 rc = send_response(conn _sd, msg_bad_request);280 return rc; 281 } 282 283 return uri_get(uri, conn _sd);291 rc = send_response(conn, msg_bad_request); 292 return rc; 293 } 294 295 return uri_get(uri, conn); 284 296 } 285 297 … … 346 358 } 347 359 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 } 375 } 376 348 377 int main(int argc, char *argv[]) 349 378 { 379 inet_ep_t ep; 380 tcp_listener_t *lst; 381 tcp_t *tcp; 382 int rc; 383 350 384 /* Parse command line arguments */ 351 385 for (int i = 1; i < argc; i++) { 352 386 if (argv[i][0] == '-') { 353 intrc = parse_option(argc, argv, &i);387 rc = parse_option(argc, argv, &i); 354 388 if (rc != EOK) 355 389 return rc; … … 360 394 } 361 395 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); 396 printf("%s: HelenOS web server\n", NAME); 397 398 if (verbose) 399 fprintf(stderr, "Creating listener\n"); 400 401 inet_ep_init(&ep); 402 ep.port = port; 403 404 rc = tcp_create(&tcp); 369 405 if (rc != EOK) { 370 fprintf(stderr, "Error parsing network address (%s)\n", 371 str_error(rc)); 406 fprintf(stderr, "Error initializing TCP.\n"); 372 407 return 1; 373 408 } 374 375 printf("%s: HelenOS web server\n", NAME); 376 377 if (verbose) 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)); 409 410 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL, 411 &lst); 412 if (rc != EOK) { 413 fprintf(stderr, "Error creating listener.\n"); 384 414 return 2; 385 }386 387 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));388 if (rc != EOK) {389 fprintf(stderr, "Error binding socket (%s)\n",390 str_error(rc));391 return 3;392 }393 394 rc = listen(listen_sd, BACKLOG_SIZE);395 if (rc != EOK) {396 fprintf(stderr, "listen() failed (%s)\n", str_error(rc));397 return 4;398 415 } 399 416 … … 402 419 403 420 task_retval(0); 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 } 421 async_manager(); 440 422 441 423 /* Not reached */
Note:
See TracChangeset
for help on using the changeset viewer.