Skip to content
Snippets Groups Projects
Select Git revision
  • 4793ed76a8ba4d0da7246c196c8982de54f0f5cb
  • master default protected
2 results

get_coverage.sh

Blame
  • Forked from Toby Murray / swen90006-a2-2020
    Source project has a limited visibility.
    server.c 13.46 KiB
    /*
    ** COMP30023_2019SM1 Project1
    ** Code completed by Xun Zhang (854776)
    **
    ** Reference
    ** "http-server.c" from Lab 6
    */
    
    #include <errno.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <arpa/inet.h>
    #include <fcntl.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <strings.h>
    #include <sys/select.h>
    #include <sys/sendfile.h>
    #include <sys/socket.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    // constants
    static char const* const HTTP_200_FORMAT =
        "HTTP/1.1 200 OK\r\n\
    Content-Type: text/html\r\n\
    Content-Length: %ld\r\n\r\n";
    static char const* const HTTP_400 =
        "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n";
    static int const HTTP_400_LENGTH = 47;
    static char const* const HTTP_404 =
        "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
    static int const HTTP_404_LENGTH = 45;
    
    static char const* const INTRO_PAGE = "./views/1_intro.html";
    static char const* const START_PAGE = "./views/2_start.html";
    static char const* const FIRST_TURN_PAGE = "./views/3_first_turn.html";
    static char const* const ACCEPTED_PAGE = "./views/4_accepted.html";
    static char const* const DISCARDED_PAGE = "./views/5_discarded.html";
    static char const* const ENDGAME_PAGE = "./views/6_endgame.html";
    static char const* const GAMEOVER_PAGE = "./views/7_gameover.html";
    
    bool is_end_game, is_quit_game;
    bool p1_is_ready, p2_is_ready;
    int p1_sockfd, p2_sockfd;
    // assume we can save 2000 keywords from each player,
    // and 200 words for each keyword
    char p1_guess[2000][200], p2_guess[2000][200];
    int p1_guess_len, p2_guess_len;
    
    // represents the types of method
    typedef enum { GET, POST, UNKNOWN } METHOD;
    
    // methods reference
    void run_server(const char* ip, const int port);
    int create_server_socket(const char* ip, const int port);
    static bool handle_http_request(int sockfd);
    bool show_page(int sockfd, const char* htmldir);
    bool show_modified_page(int sockfd, const char* htmldir, char* added,
                            int inset_index);
    bool show_start_page(int sockfd, char* username);
    bool show_accepted_page(int sockfd);
    void print_buff(char* request); // test method
    bool keyword_check(int sockfd, char* keyword);
    void player_init();