Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* indexes(const char* path)
- {
- DIR *dir_ptr = opendir(path);
- while(readdir(dir_ptr) != NULL)
- {
- struct dirent* dir_f = readdir(dir_ptr);
- if(strcmp(dir_f->d_name, "index.php") == 0)
- {
- char* path_p = malloc(strlen(path) + 1 + 9);
- strcpy(path_p, path);
- strcat(path_p, "index.php");
- return path_p;
- free(path_p);
- break;
- }
- else if(strcmp(dir_f->d_name, "index.html") == 0)
- {
- char* path_h = malloc(strlen(path) + 1 + 10);
- strcpy(path_h, path);
- strcat(path_h, "index.html");
- return path_h;
- free(path_h);
- break;
- }
- else
- break;
- }
- return NULL;
- }
- bool load(FILE* file, BYTE** content, size_t* length)
- {
- BYTE *buf = NULL;
- int counter = 0;
- while(feof(file)==0)
- {
- fread(buf,sizeof(BYTE), 1, file);
- counter++;
- }
- *length = counter;
- // array for loading
- BYTE *b_array;
- b_array = (BYTE *)malloc(sizeof(BYTE)*counter);
- // set file start point
- fseek(file, 0, SEEK_SET);
- *content = b_array;
- int i = 0;
- while(feof(file)==0)
- {
- fread(buf,sizeof(BYTE), 1, file);
- b_array[i] = *buf;
- i++;
- }
- return true;
- }
- const char* lookup(const char* path)
- {
- // char pointer to find dot
- char* dot_ptr;
- // array to store file type extension
- char ext[6];
- int i = 0;
- dot_ptr = strrchr(path,'.');
- do
- {
- ext[i] = *dot_ptr;
- i++;
- dot_ptr++;
- }while(*dot_ptr != '\0');
- ext[i+1] = '\0';
- if(strcasecmp(".css", ext) == 0)
- return "text/css";
- else if(strcasecmp(".html", ext) == 0)
- return "text/html";
- else if(strcasecmp(".gif", ext) == 0)
- return "image/gif";
- else if(strcasecmp(".ico", ext) == 0)
- return "image/x-icon";
- else if(strcasecmp(".jpg", ext) == 0)
- return "image/jpeg";
- else if(strcasecmp(".js", ext) == 0)
- return "text/javascript";
- else if(strcasecmp(".php", ext) == 0)
- return "text/x-php";
- else if(strcasecmp(".png", ext) == 0)
- return "image/png";
- else
- return NULL;
- }
- bool parse(const char* line, char* abs_path, char* query)
- {
- char* ptr_line;
- // counter for space
- int sp_count = 0;
- for(ptr_line = strchr(line,'G'); *ptr_line != 13; ptr_line++) // 13 = CR
- {
- if(*ptr_line == 32) // equals to space
- sp_count++;
- }
- if(sp_count != 2)
- {
- error(400);
- return false;
- }
- char get[] = "GET ";
- // compare 4 characters between line and get
- if(strncmp(get, line, 4) != 0)
- {
- error(405);
- return false;
- }
- // pointer for first space
- char* sp_ptr;
- sp_ptr = strchr(line, 32); //32 = space
- sp_ptr++; // one character forward
- if(*sp_ptr != '/')
- {
- error(501);
- return false;
- }
- while(*sp_ptr != 32) // space
- {
- if(*sp_ptr == 34) // double quotation
- {
- error(400);
- return false;
- }
- sp_ptr++;
- }
- // one character forward
- sp_ptr++;
- char http[] = "HTTP/1.1";
- if(strncmp(sp_ptr, http, 8) != 0)
- {
- error(505);
- return false;
- }
- char ab_pass[LimitRequestLine+1];
- int i = 0;
- char* slash_ptr;
- slash_ptr = strchr(line, '/');
- while(*slash_ptr != 32) // 32 = space
- {
- ab_pass[i] = *slash_ptr;
- i++;
- slash_ptr++;
- }
- ab_pass[i+1] = '\0';
- strcpy(abs_path, ab_pass);
- //array to store query
- char qu[LimitRequestLine+1];
- //pointer of ?
- char* quest_ptr = strchr(line, '?');
- if(quest_ptr == NULL)
- qu[0] = '\0';
- else
- {
- quest_ptr++; // one character forward
- if(*quest_ptr == 32) //32 space
- qu[0] = '\0';
- else
- {
- int index = 0;
- char* quest_e_ptr = strstr(line, "q=");
- if(quest_e_ptr != NULL)
- {
- while(*quest_e_ptr != 32) // 32 space
- {
- qu[index] = *quest_e_ptr;
- quest_e_ptr++;
- index++;
- }
- qu[index+1] = '\0';
- }
- }
- }
- strcpy(query, qu);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement