Skip to content
Snippets Groups Projects
Commit 6f6caf9a authored by Paul Ripault's avatar Paul Ripault
Browse files

7.1 rewrite fonctionne

parent bcf0f38e
Branches
Tags 10-404-clean
No related merge requests found
CFLAGS=-Wall -Wextra -Werror -g
LIB=libhttp_parse.a
SRC=$(wildcard *.c)
OBJS=$(SRC:.c=.o)
HDR=$(wildcard *.h)
$(LIB): $(OBJS)
ar rs $@ $^
ranlib $@
makefile.dep: $(SRC) $(HDR)
$(CC) $(CFLAGS) -MM $(SRC) > $@
.PHONY: clean mrproper
clean:
$(RM) $(OBJS)
mrproper: clean
$(RM) $(LIB)
include makefile.dep
\ No newline at end of file
File added
http_parse.o: http_parse.c http_parse.h
......@@ -9,10 +9,11 @@
#include "http_parse.h"
char* rewrite_target(char *target){
char* ret = strchr(target, '?');
char* rep ="";
char* ret = strrchr(target, '?');
char* rep = malloc(strlen(target)*sizeof(char));
if (ret!= NULL){
strncpy(rep,target,atoi(ret)-1);
size_t i = (int)(strlen(target)-strlen(ret));
rep = strncpy(rep,target,i);
}
return rep;
}
......@@ -51,13 +52,11 @@ void send_status(FILE *client, int code, const char *reason_phrase){
}
void send_response(FILE *client, int code, const char *reason_phrase, const char *message_body){
fprintf(client, "entree dans send_response\n");
send_status(client,code,reason_phrase);
//transmet la réponse complète (la suite)
fprintf(client,"Content-Lenght: %u\r\n",(unsigned int)strlen(message_body));
fprintf(client,"\r\n");
fprintf(client,"%s\r\n",message_body);
fprintf(client, "sortie de send_response\n");
fflush(client);
}
......@@ -79,25 +78,24 @@ int traitementClient(int socket_client){
fgets_or_exit(str,8000,fd);
int parse_ret = parse_http_request(str,&request);
skip_headers(fd);
fprintf(fd, "avant if parse_ret\n");
if (parse_ret == -1) {
if (request.method == HTTP_UNSUPPORTED){
send_response(fd, 405, "Method Not Allowed", "Method Not Allowed");
}else{
send_response(fd, 400, "Bad Request", "Bad request");
}
}else if (strcmp(request.target, "/") == 0){
}else if (strcmp(request.target, "/test?aled") == 0){
send_response(fd, 200, "OK", motd);
fprintf(fd, "fin du else-if\n");
}else{
send_response(fd, 404, "Not Found", "Not Found");
}
fflush(fd);
fprintf(fd, "après if parse_ret\n");
//partie 7
fprintf(fd, "avant rewrite\n");
char* absolute_path = rewrite_target(request.target);
fprintf(fd,"test - - - - - - - - %s\n",absolute_path);
printf("test - - - - - - - - %s\n",absolute_path);
fclose(fd);
return 0;
}
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment