Implement get_file_from_url
parent
cf1d164ec5
commit
dcc1ca1a2a
|
@ -1,4 +1,5 @@
|
|||
#include "request_handling.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -78,3 +79,35 @@ int parse_request_line(const char *line, struct request_line *parsed_line) {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a FILE from a given uri.
|
||||
*
|
||||
* @param uri The uri in the request line.
|
||||
* @param req_file A pointer to a requested_file struct.
|
||||
*
|
||||
* @return -1 For an I/O error, 0 if successful.
|
||||
*/
|
||||
int get_file_from_url(const char *uri, struct requested_file *req_file) {
|
||||
// If our uri starts with a slash, we can ignore it.
|
||||
if (*uri == '/') {
|
||||
uri++;
|
||||
}
|
||||
FILE *open_file = fopen(uri, "r");
|
||||
if (open_file == NULL) {
|
||||
return -1;
|
||||
}
|
||||
int seek_result = fseek(open_file, 0, SEEK_END);
|
||||
if (seek_result != 0) {
|
||||
return -1;
|
||||
}
|
||||
long size = ftell(open_file);
|
||||
if (size == -1) {
|
||||
return -1;
|
||||
}
|
||||
rewind(open_file);
|
||||
req_file->size = size;
|
||||
req_file->file = open_file;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,20 @@
|
|||
#ifndef REQUEST_HANDLING_H
|
||||
#define REQUEST_HANDLING_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct request_line {
|
||||
char *http_version;
|
||||
char *method;
|
||||
char *uri;
|
||||
};
|
||||
|
||||
struct requested_file {
|
||||
long size;
|
||||
FILE *file;
|
||||
};
|
||||
|
||||
int parse_request_line(const char *line, struct request_line *parsed_line);
|
||||
int get_file_from_url(const char *uri, struct requested_file *req_file);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue