server.c
int send_reply(int socket_fd, const char *request)
{
    char reply[MAX_REPLY] = "Default Reply.";
    char unknown[] = "Unknown request.\n";

    char buffer[1024];
    char path[1024]; //TODO dynamic
    char length[256];
    char GET[4];
    int i;

    strcpy(GET, "GET");

    /* Check the type of request */
    if (strncmp(GET, request, 3) == 0)
    {
        printf("received GET\n");

        /* Get the filename */
        for (i = 4; i < strlen(request); ++i)
        {
            if (isspace(request[i]))
            {
              path[i - 4] = '\0';
              break;
            }
            else
            {
              path[i - 4] = request[i];
            }

        }

        printf("Path requested: %s\n", path);

        /* Route root to index.html */
        if (strcmp(path, "/") == 0) {
          strcpy(path, "index.html");
        }

        /* Try to load the requested file and reply */
        if (load_file(path, buffer, 1024) == 0)
        {
            strcpy(reply, "HTTP/1.1 200 OK\n");
            strcat(reply, "Content-Type: text/html\n");
            sprintf(length, "Content-Length: %ld\n\n", strlen(buffer)); // TODO smaller + buffer
            strcat(reply, length);
            strcat(reply, buffer); 
        }
        else
        {
            strcpy(reply, "HTTP/1.1 404 Not Found\n");
        }


    }
    else
    {
        strcpy(reply, unknown);
    }

    printf("Sending %ld:\n'%s'\n", strlen(reply), reply);
    write(socket_fd, reply, strlen(reply));

    return 0;
}
/* Sends a 200 or 404 appropriate to the request. */
Sends a 200 or 404 appropriate to the request.
Sends a 200 or 404 appropriate to the request .
int send_reply(int socket_fd, const char *request)
{
    char reply[MAX_REPLY] = "Default Reply.";
    char unknown[] = "Unknown request.\n";
    char buffer[1024];
    char path[1024]; 
    char length[256];
    char GET[4];
    int i;
    strcpy(GET, "GET");
    if (strncmp(GET, request, 3) == 0)
    {
        printf("received GET\n");
        for (i = 4; i < strlen(request); ++i)
        {
            if (isspace(request[i]))
            {
              path[i - 4] = '\0';
              break;
            }
            else
            {
              path[i - 4] = request[i];
            }
        }
        printf("Path requested: %s\n", path);
        if (strcmp(path, "/") == 0) {
          strcpy(path, "index.html");
        }
        if (load_file(path, buffer, 1024) == 0)
        {
            strcpy(reply, "HTTP/1.1 200 OK\n");
            strcat(reply, "Content-Type: text/html\n");
            sprintf(length, "Content-Length: %ld\n\n", strlen(buffer)); 
            strcat(reply, length);
            strcat(reply, buffer); 
        }
        else
        {
            strcpy(reply, "HTTP/1.1 404 Not Found\n");
        }
    }
    else
    {
        strcpy(reply, unknown);
    }
    printf("Sending %ld:\n'%s'\n", strlen(reply), reply);
    write(socket_fd, reply, strlen(reply));
    return 0;
}
int send_reply ( int socket_fd , const char * request ) { char reply [ MAX_REPLY ] = " " ; char unknown [ ] = " \n " ; char buffer [ 1024 ] ; char path [ 1024 ] ; char length [ 256 ] ; char GET [ 4 ] ; int i ; strcpy ( GET , " " ) ; if ( strncmp ( GET , request , 3 ) == 0 ) { printf ( " \n " ) ; for ( i = 4 ; i < strlen ( request ) ; ++ i ) { if ( isspace ( request [ i ] ) ) { path [ i - 4 ] = ' \0 ' ; break ; } else { path [ i - 4 ] = request [ i ] ; } } printf ( " \n " , path ) ; if ( strcmp ( path , " " ) == 0 ) { strcpy ( path , " " ) ; } if ( load_file ( path , buffer , 1024 ) == 0 ) { strcpy ( reply , " \n " ) ; strcat ( reply , " \n " ) ; sprintf ( length , " \n \n " , strlen ( buffer ) ) ; strcat ( reply , length ) ; strcat ( reply , buffer ) ; } else { strcpy ( reply , " \n " ) ; } } else { strcpy ( reply , unknown ) ; } printf ( " \n \n " , strlen ( reply ) , reply ) ; write ( socket_fd , reply , strlen ( reply ) ) ; return 0 ; }
Sends a 200 or 404 appropriate to the request.
Sends a 200 or 404 appropriate to the request .
//TODO dynamic /* Check the type of request */ /* Get the filename */ /* Route root to index.html */ /* Try to load the requested file and reply */ // TODO smaller + buffer