0

server.c is giving error(403) inside transfer function (written by default)

lookup function :

const char* lookup(const char* path)
{
    char* s=(char*)calloc(20,1);
    strcpy(s,"");

    char *path1=(char*)calloc((strlen(path)+1),1);
    for(int i=0;i<strlen(path);i++)
    {
        path1[i]=tolower(path[i]);
    }
    printf("path1 : %s",path1);
    for(int i=0;i<strlen(path1);i++)
    {  
        if(path1[i]=='.')
            {
                s[0]=path1[i];
                s[1]=path1[i+1];
                s[2]=path1[i+2];
                s[3]='\0';
                break;
            }


    }

   // s[3]='\0';
    if(!strcmp(s,".cs"))
    {
        strcpy(s,"text/css");
        return(s);
    }
    if(!strcmp(s,".ht"))
    {
        strcpy(s,"text/html");
        return(s);
    }if(!strcmp(s,".gi"))
    {
        strcpy(s,"image/gif");
        return(s);
    }if(!strcmp(s,".ic"))
    {
        strcpy(s,"image/x-icon");
        return(s);
    }if(!strcmp(s,".jp"))
    {
        strcpy(s,"image/.jpeg");
        return(s);
    }if(!strcmp(s,".js"))
    {
        strcpy(s,"text/javascript");
        return(s);
    }if(!strcmp(s,".ph"))
    {
        strcpy(s,"text/x-php");
        return(s);
    }if(!strcmp(s,".pn"))
    {

        strcpy(s,"image/png");
        return(s);
    }

    return NULL;


}

parse function :

bool parse(const char* line, char* abs_path, char* query)
{


    for(int i=0;i<strlen(line);i++)
    {
        if(line[i]==34)
        {
            error(400);
            return false;
        }
    }
    if(line[0]!='G'||line[1]!='E'||line[2]!='T'||line[3]!=32)
    {
        error(405);
        return false;
    }
    if(line[4]!='/')
    {
        printf("*Here");
        error(501);
        return false;
    }
  //  int f=0;


    for(int i=0;i<strlen(line);i++)
    {
        int flag=0,j=0,q=0;
        if(line[i]==' ')
        {   
            flag=0;
            q++;

        }
        if(line[i]=='/')
        {
            flag=1;

        }
        if(flag)
        {
            abs_path[j]=line[i];
            j++;
        }

        if(q==2)
        {
            abs_path[j]='\0';
            break;
        }

        if(line[i]=='?')
        {
          abs_path[j]='\0';
          j=0;
          for(int e=i+1;line[e]!=' ';e++)
          {
              query[j]=line[e];
              j++;
          }
          query[j]='\0';
          break;  
        }
    }
 int f=0;
    for(int i=0;i<strlen(line);i++)
    {
        if(line[i]==' ')
            f++;
        if(f==2)
        {
            if(line[i+1]!='H'||line[i+2]!='T'||line[i+3]!='T'||line[i+4]!='P'||line[i+5]!='/'||line[i+6]!='1'||line[i+7]!='.'||line[i+8]!='1')
            {
                f++;
                printf("%c %c %c %c %c %c %c %c",line[i+1],line[i+2],line[i+3],line[i+4],line[i+5],line[i+6],line[i+7],line[i+8]);
                error(505);
                return false;
            }
            else
            break;
        }
    }   
    return true;
}

My valgrind is giving this error :

==6796== Conditional jump or move depends on uninitialised value(s)
==6796==    at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6796==    by 0x4029EA: urldecode (server.c:1178)
==6796==    by 0x4018A3: main (server.c:191)
==6796== 
==6796== Conditional jump or move depends on uninitialised value(s)
==6796==    at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6796==    by 0x402A32: urldecode (server.c:1186)
==6796==    by 0x4018A3: main (server.c:191)
==6796== 
==6796== Conditional jump or move depends on uninitialised value(s)
==6796==    at 0x4C2E0F8: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6796==    by 0x4019EE: main (server.c:221)
==6796== 

I can't figure out the exact problem but I think it is somewhere related to abs_path and urldecode function. Please Help

1
  • Same issues but check50 says: ( Requesting non-existant file returns error code 404 \ expected output, but not "HTTP/1.1 501 Not Implemented\r\nContent..." ... – Parth Gupta Jun 25 '16 at 11:46
1
strcpy(s,lookup(line));

After gdb: Program received signal SIGSEGV, Segmentation fault. __strcpy_ssse3 () at ../sysdeps/i386/i686/multiarch/strcpy-ssse3.S:84

I would look for another way, for example:

// copy line to new string
    char* temp = malloc(strlen(line) + 1);
    if(temp == NULL)
    {
        free(temp);
        return false;
    }
    strcpy(temp, line);

i don't know how is possible to resolve server without using gdb, this is a fundamental skill that you have to learn. I also do not know what you intend to use lookup, if only returns a string with the MIME type of file, you have to analyze line and see that it fulfills the specifications

EDIT

Come to lookup a pointer question: `const char* lookup(const char* path), means that lookup, receives a constant pointer, really it means that we have a string, which already exists and will not change, there is no need to malloc, calloc or realloc; const char * path means I have a pointer to the start of the string path, but can also access each and every one of his characters. We stopped for a moment before writing code "I just need to find a characteristic of path, that is useful to me to determine the file extension".

if you look a time all file extensions have a point: '.' and now we see a little problem specifications suggest us use features like strcasecmp, strcpy, and / or strrchr, it is our duty to learn to use them. char * strrchr (const char * s, int c); It is perfect for us, returns a pointer to the last occurrence of the character c in the string s. We have function (strrchr), we have the character we want( a point) and we have a constant string ( const char* path). therefore we can do:

char * point = strrchr (path, '.'); // Returns a pointer to the character point

point is a string (char *), so if exists point we have a chain that begins in that character, ie if path ends with .html point is precisely ".html" exactly what we want. Now we can use a very useful function in this case strcasecmp, our chain point, and a lot of IF conditions, I think this is enough to start.

EDIT 2

if (abs_path[strlen(abs_path) - 1] != '/')                       
  char* index = indexes(path);

Are the lines of code that cause the error because abs_path is not correct, and strlen is 0. Parse does not work, I understand your idea, but not executed correctly.

for(int i=0;i<strlen(line);i++)
    {
        int flag=0,j=0,q=0;

Every time begins FOR, your control variables initialized zero, so not fulfill its function and abs_path is '\' at the end of parse.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

14
  • Thanks for your response. But I am getting seg fault while using gdb. Exact error : "Program received signal SIGSEGV, Segmentation fault. __strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296 296 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory." Do you have any idea about what's the error? – Salil Jain Jun 27 '16 at 12:09
  • I think you can ignore this gdb "error" , The error is simply because GDB is looking for the source of the strcpy function, but can not find, however it is an assumption, use clang to compile, or better make in the IDE – MARS Jun 27 '16 at 14:24
  • Sorry, What do you mean by saying use clang or make? – Salil Jain Jun 27 '16 at 15:02
  • some person, per usual, using gcc to compile a program, you must use "make server" or "clang -ggdb3 -O0 -std = c11 -Wall -o server -Werror server.c -lm". Please post your new code if you continue having problems – MARS Jun 27 '16 at 17:33
  • I have modified my code a bit, firstly my lookup function was wrong, it copied "htm" to string 's' instead of ".ht".Now it is giving error 403 Forbidden by access function under transfer function(which are written by default). My path and type are correct which I checked using printf which means access function is not working properly. Do you know what's the problem? – Salil Jain Jun 28 '16 at 18:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .