1

所以我在下面发布了我的代码(对不起,我知道它很长),但是当我尝试使用 tm 结构执行任何操作时出现分段错误。我不知道为什么会出现此段错误,我很确定我以前可以正常工作,但是现在我无法正常工作。如果有人知道如何摆脱段错误,那就太好了。段错误出现在第一个嵌套的 if 语句中,位于代码块的中下部,它位于 if-else 的长块之后。

非常感谢!

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <time.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>


int main(){

int sockfd, clifd, status, monthModifiedi, newer;
struct sockaddr_in addr;
//char buf[1024] = "HEAD httpserver.c HTTP/1.1\nHost: www.reddit.com";
char buf[1024];
struct stat statBuf;
char *message, *filePath, *domain, *monthModified, *dayModified, *hourModified, *minuteModified, *secondModified, *yearModified;
FILE *pFile;
struct tm *tm;
time_t time;

//instantiate
strcpy(buf,"HEAD /httpserver.c HTTP/1.1\nHost: www.reddit.com\nIf-Modified-Since: Thu Mar 28 02:20:08 2013");

printf("%s\n\n", buf);

//parse buffer
message = strtok(buf, " ");
filePath = strtok(NULL, " ");
domain = strtok(NULL, "\n");
domain = strtok(NULL, " "); //domain will be overwritten with actual doman, this is filler
domain = strtok(NULL, " \n");
monthModified = strtok(NULL, " ");

printf("%s\n", filePath);
if(stat(filePath, &statBuf) == 0){
    tm = localtime(&statBuf.st_mtime);
    //time = &statBuf.st_mtime;
}

//newer will keep this value of 2 if there is no conditional GET request
newer = 2;

//If it is a conditional GET request
if(strcmp(monthModified, "If-Modified-Since:") == 0){

    monthModified = strtok(NULL, " ");
    monthModified = strtok(NULL, " ");
  dayModified = strtok(NULL, " ");
  hourModified = strtok(NULL, " :");
  minuteModified = strtok(NULL,":");
  secondModified = strtok(NULL,": ");
  yearModified = strtok(NULL, " ");

    //Turns string version of month to numeric value
    if(strcmp(monthModified,"Jan") ==0)
        monthModifiedi = 1;
    else if(strcmp(monthModified,"Feb")==0)
        monthModifiedi = 2;
    else if(strcmp(monthModified,"Mar") ==0)
        monthModifiedi = 3;
    else if(strcmp(monthModified,"Apr") ==0)
        monthModifiedi = 4;
    else if(strcmp(monthModified,"May") ==0)
        monthModifiedi = 5;
    else if(strcmp(monthModified,"Jun") ==0)
        monthModifiedi = 6;
    else if(strcmp(monthModified,"Jul") ==0)
        monthModifiedi = 7;
    else if(strcmp(monthModified,"Aug") ==0)
        monthModifiedi = 8;
    else if(strcmp(monthModified, "Sep") ==0)
        monthModifiedi = 9;
    else if(strcmp(monthModified,"Oct") ==0)
        monthModifiedi = 10;
    else if(strcmp(monthModified, "Nov") ==0)
        monthModifiedi = 11;
    else if(strcmp(monthModified,"Dec") ==0)
        monthModifiedi = 12;

    //Determines if file has been modified since date requested from client
    newer = 0;
    if(atoi(yearModified) > (*tm).tm_year)
        if(monthModifiedi > (*tm).tm_mon)
            if(atoi(dayModified) > (*tm).tm_mday)
                if(atoi(hourModified) > (*tm).tm_hour)
                    if(atoi(minuteModified) > (*tm).tm_min)
                        if(atoi(secondModified) > (*tm).tm_sec)
                            newer = 1;
}

printf("Value of newer: %i", newer);
printf("Message: %s Filename: %s Domain: %s monthModified: %i dayModified: %s hourModified: %s minuteModified: %s secondModified: %s yearModified: %s\n", message, filePath, domain, monthModifiedi, dayModified, hourModified, minuteModified, secondModified, yearModified);

if(message == "GET"){
    if ((pFile = fopen(filePath, "w")) == NULL){
        message = "HTTP/1.1 404 Not Found\n\r";
        send(clifd, message, strlen(message), 0);
    }
    // If not conditinal request, or if file has been modified
    else if(newer == 2 || newer == 1){
        message = "HTTP/1.1 200 OK\n\r", pFile;
        send(clifd, message, strlen(message), 0);
    }
    // If file has not been modified
    else{
        message = "HTTP/1.1 304 Not Modified\n\r";
        send(clifd, message, strlen(message),0);
    }
}
else if(message == "HEAD"){
    if ((pFile = fopen(filePath, "w")) == NULL){
        message = "HTTP/1.1 404 Not Found\n\r";
        send(clifd, message, strlen(message), 0);
    }
    else{
        message = "HTTP/1.1 200 OK\n\r";
        send(clifd, message, strlen(message), 0);
    }
}

return 0;
}
4

2 回答 2

1

的返回值是stat多少?

if(stat(filePath, &statBuf) == 0){
    tm = localtime(&statBuf.st_mtime);
    //time = &statBuf.st_mtime;
}

您仅初始化tm是否stat成功,但如果不是,则无论如何都可以访问它而无需检查。

于 2013-04-10T23:25:30.640 回答
1

问题不在于它tm本身,而在于filePath. 您很可能在根目录中没有名为/httpserver.c. 这导致分配

tm = localtime(&statBuf.st_mtime);

被跳过并tm指向任意内存。

从字符串中,我猜这应该是一个 HTTP HEAD 请求。要测试正确的文件,您必须将文档根添加到请求的 URI 的名称之前。

于 2013-04-10T23:25:48.247 回答