[f4a2d624] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup websrv
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file (Less-than-skeleton) web server.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <stdio.h>
|
---|
| 37 |
|
---|
| 38 | #include <net/in.h>
|
---|
| 39 | #include <net/inet.h>
|
---|
| 40 | #include <net/socket.h>
|
---|
| 41 |
|
---|
| 42 | #include <str.h>
|
---|
| 43 |
|
---|
| 44 | #define PORT_NUMBER 8080
|
---|
| 45 |
|
---|
| 46 | /** Buffer for receiving the request. */
|
---|
| 47 | #define BUFFER_SIZE 1024
|
---|
| 48 | static char buf[BUFFER_SIZE];
|
---|
| 49 |
|
---|
| 50 | /** Response to send to client. */
|
---|
| 51 | static const char *response_msg =
|
---|
| 52 | "HTTP/1.0 200 OK\r\n"
|
---|
| 53 | "\r\n"
|
---|
| 54 | "<h1>Hello from HelenOS!</h1>\r\n";
|
---|
| 55 |
|
---|
| 56 | int main(int argc, char *argv[])
|
---|
| 57 | {
|
---|
| 58 | struct sockaddr_in addr;
|
---|
| 59 | struct sockaddr_in raddr;
|
---|
| 60 |
|
---|
| 61 | socklen_t raddr_len;
|
---|
| 62 |
|
---|
| 63 | int listen_sd, conn_sd;
|
---|
| 64 | int rc;
|
---|
| 65 |
|
---|
| 66 | size_t response_size;
|
---|
| 67 |
|
---|
| 68 | addr.sin_family = AF_INET;
|
---|
| 69 | addr.sin_port = htons(PORT_NUMBER);
|
---|
| 70 |
|
---|
| 71 | rc = inet_pton(AF_INET, "127.0.0.1", (void *) &addr.sin_addr.s_addr);
|
---|
| 72 | if (rc != EOK) {
|
---|
| 73 | printf("Error parsing network address.\n");
|
---|
| 74 | return 1;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | printf("Creating socket.\n");
|
---|
| 78 |
|
---|
| 79 | listen_sd = socket(PF_INET, SOCK_STREAM, 0);
|
---|
| 80 | if (listen_sd < 0) {
|
---|
| 81 | printf("Error creating listening socket.\n");
|
---|
| 82 | return 1;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));
|
---|
| 86 | if (rc != EOK) {
|
---|
| 87 | printf("Error binding socket.\n");
|
---|
| 88 | return 1;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | rc = listen(listen_sd, 1);
|
---|
| 92 | if (rc != EOK) {
|
---|
| 93 | printf("Error calling listen() (%d).\n", rc);
|
---|
| 94 | return 1;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | response_size = str_size(response_msg);
|
---|
| 98 |
|
---|
| 99 | printf("Listening for connections at port number %u.\n", PORT_NUMBER);
|
---|
| 100 | while (true) {
|
---|
| 101 | raddr_len = sizeof(raddr);
|
---|
| 102 | conn_sd = accept(listen_sd, (struct sockaddr *) &raddr,
|
---|
| 103 | &raddr_len);
|
---|
| 104 |
|
---|
| 105 | if (conn_sd < 0) {
|
---|
| 106 | printf("accept() failed.\n");
|
---|
| 107 | return 1;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | printf("Accepted connection, sd=%d.\n", conn_sd);
|
---|
| 111 |
|
---|
| 112 | printf("Wait for client request\n");
|
---|
| 113 |
|
---|
| 114 | /* Really we should wait for a blank line. */
|
---|
| 115 | rc = recv(conn_sd, buf, BUFFER_SIZE, 0);
|
---|
| 116 | if (rc < 0) {
|
---|
| 117 | printf("recv() failed\n");
|
---|
| 118 | return 1;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /* Send a canned response. */
|
---|
| 122 | printf("Send response...\n");
|
---|
| 123 | rc = send(conn_sd, (void *) response_msg, response_size, 0);
|
---|
| 124 | if (rc < 0) {
|
---|
| 125 | printf("send() failed.\n");
|
---|
| 126 | return 1;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | rc = closesocket(conn_sd);
|
---|
| 130 | if (rc != EOK) {
|
---|
| 131 | printf("Error closing connection socket: %d\n", rc);
|
---|
| 132 | return 1;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | printf("Closed connection.\n");
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /* Not reached. */
|
---|
| 139 | return 0;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /** @}
|
---|
| 143 | */
|
---|