Example #1
0
void http_request_startServer(pupils_t pupils){
    lib_init();
    socket_t * serverSocket = socket_new();
    socket_bind(serverSocket, 5000);
    socket_listen(serverSocket);
    while(1){
        puts("Waiting for connections");
        socket_t * clientSocket = socket_accept(serverSocket);
        puts("New client");
        char buff[BUFFER_LENGTH];
        int readLength = socket_read(clientSocket, buff, BUFFER_LENGTH);
        if(readLength == 0){
            socket_close(clientSocket);
            socket_free(clientSocket);
            puts("Skipping empty request");
            continue;
        }

        printf("Got Request:\n---------------\n%s\n----------------\n", buff);

        http_request_t req = http_request_parse(buff);

        printf("Method: %s\nURI: %s\n", req.method, req.uri);
        puts("Data:");
        for(int i = 0; i < req.formLength; i++){
            char * kvStr = keyvalue_toString(&req.form[i]);
            printf("\t%s\n", kvStr);
            free(kvStr);
        }
        http_request_chooseMethod(req, clientSocket, pupils);
        socket_close(clientSocket);
        socket_free(clientSocket);
    }
    socket_close(serverSocket);
    socket_free(serverSocket);
    lib_free();
}
Example #2
0
File: server.c Project: r0sen/r0sen
void startServer(list_t *list)
{
    lib_init();
    socket_t * serverSocket = socket_new();
    socket_bind(serverSocket, 5000);
    socket_listen(serverSocket);
    while(1)
    {
        puts("Waiting for connections");
        socket_t * clientSocket = socket_accept(serverSocket);
        char buff[BUFFER_LENGTH];
        int readLength = socket_read(clientSocket, buff, BUFFER_LENGTH);
        if(readLength == 0)
        {
            socket_close(clientSocket);
            socket_free(clientSocket);
            puts("Skipping empty request");
            continue;
        }

        printf("Got Request:\n---------------\n%s\n----------------\n", buff);

        http_request_t req = http_parse(buff);

        printf("Method: %s\nURI: %s\n", req.method, req.uri);
        puts("Data:");
        for(int i = 0; i < req.formLength; i++)
        {
            char * kvStr = keyvalue_toString(&req.form[i]);
            printf("\t%s\n", kvStr);
            free(kvStr);
        }


   if(!strcmp(req.uri, "/"))
    {
        char smallMSG[100];
        sprintf(smallMSG, "<h1>HELLO ALL!!!</h1>");
        char result_msg[MSG_LENGTH];
        //resp_form(HTML, smallMSG, 200, result_msg);
                    sprintf(result_msg,
                    "HTTP/1.1 200 OK\n"
                    "Content-length: %zu\n"
                    "Content-type: application/xml\n"
                    "\n"
                    "%s\0", strlen(smallMSG), smallMSG);
        socket_write_string(clientSocket, result_msg);
    }
    else if(!strcmp(req.uri, "/info"))
    {
        char result_msg[MSG_LENGTH];
        resp_form(XML, first_task_func(), 200, result_msg);

        socket_write_string(clientSocket, result_msg);
    }

    else if (!strcmp(req.uri, "/external"))
        {
        static const char * requestFormat =
        "%s %s HTTP/1.1\r\n"
        "Content-Type: text\r\n"
        "Content-Length: %zu\r\n\r\n"
        "%s";
        socket_t* serverSock = socket_new ();
        socket_connect(serverSock, "216.58.209.49", 80);
        char uri [256];
        strcpy (uri, "http://pb-homework.appspot.com/test/var/5?format=xml");
        char req [1024];
        sprintf (req, requestFormat, "GET", uri, NULL, NULL, NULL);
        socket_write(serverSock, req, strlen (req));
        char responce [1024];
        socket_read (serverSock, responce, 1024);
        int contentLength = 0;
        sscanf (strstr(responce, "Content-Length: ") + strlen ("Content-Length: "), "%d", &contentLength);
        char* data = strstr (responce, "\r\n\r\n") + 4;
        socket_free(serverSock);
        data = second_task_func(data);
        char result_msg[MSG_LENGTH];
        resp_form(XML, data, 200, result_msg);

        socket_write_string(clientSocket, result_msg);
        }
    else
    {
        char result_msg[MSG_LENGTH];
        resp_form(XML, NULL, 404, result_msg);
        socket_write_string(clientSocket, result_msg);
        return;
    }

        socket_close(clientSocket);
        socket_free(clientSocket);
    }
    socket_close(serverSocket);
    socket_free(serverSocket);
    lib_free();
}