Refactor the client

This commit is contained in:
Ivaylo Ivanov 2018-11-27 11:29:57 +01:00
parent 712e3910ad
commit 72fb0784bf

View File

@ -36,7 +36,9 @@ int main(int argc, char *argv[]) {
char *port = "80";
char *ofile = NULL;
int opt_f = 0;
char *dir = NULL;
int opt_d = 0;
char *url = NULL;
int c;
@ -48,44 +50,61 @@ int main(int argc, char *argv[]) {
break;
case 'o':
ofile = optarg;
opt_f = 1;
break;
case 'd':
dir = optarg;
opt_d = 1;
break;
case 'h':
printf("Command usage:\n");
print_usage();
exit(EXIT_SUCCESS);
break;
case '?':
print_usage();
exit(EXIT_FAILURE);
break;
default:
abort();
}
}
/// Check for the case that both output file and directory are set
if(ofile != NULL && dir != NULL) {
printf("Either output file or directory is specified. You cannot use both.\n");
if(opt_d == 1 && opt_f == 1) {
printf("Either output file or directory should be specified. You cannot use both.\n");
print_usage();
exit(EXIT_FAILURE);
}
/// Check if the required argument is there
if(argv[optind] == NULL) {
fprintf(stderr, "Mandatory argument 'url' missing.\n");
print_usage();
exit(EXIT_FAILURE);
} else {
url = argv[optind];
}
char proto[8];
memcpy(proto, url, 7);
proto[7] = 0;
if(strcmp(proto, "http://") != 0){
printf("ERROR: The url doesn't start with http://\n");
print_usage();
}
char *base_url = strstr(url, "http://") + 7; //< the base url starts after http://
char *path = malloc(strlen(base_url) + 1);
strcpy(path, base_url); //< Copying so that strtok() doesn't change it
path = strstr(path, "/");
if(path == NULL) path = "/"; //< Set the root path if there is none
if(path == NULL) {
path = "/"; //< Set the root path if there is none
if(ofile == NULL && dir != NULL)
ofile = "index.html";
} else {
if(ofile == NULL && dir != NULL) {
ofile = strchr(path, '/');
}
}
const char *hostname = strtok(base_url, ";/?:@=&"); //< Get the FQDN of the remote
struct addrinfo* config = get_host_info(hostname, port); //< Check if the address is resolvable
@ -120,7 +139,9 @@ int main(int argc, char *argv[]) {
}
static void print_usage(void) {
printf("client [-p PORT] [ -o FILE | -d DIR ] URL\n");
puts("Usage:");
puts("client [-p PORT] [ -o FILE | -d DIR ] URL\n");
exit(EXIT_FAILURE);
}
/**
@ -224,43 +245,33 @@ static void process_response(char reply[sizeof(int32_t)], char *ofile, char *dir
char *body = strstr(reply, "\r\n\r\n") + 4;
if(ofile == NULL && dir == NULL) printf("%s", body); //< Print the body of the response
else {
if(ofile != NULL) {
FILE *out = NULL;
if(ofile != NULL && dir == NULL) {
out = fopen(ofile, "w");
if(out == NULL) {
(void) fprintf(stderr, "ERROR: Could not open ./%s for writing.", ofile);
exit(EXIT_FAILURE);
} else {
fprintf(out, "%s", body);
fwrite(body, 1, strlen(body), out);
fclose(out);
printf("File ./%s successfully written.\n", ofile);
}
} else if(dir != NULL) {
FILE *out = NULL;
} else if(ofile != NULL && dir != NULL) {
char path[sizeof(dir) + 100];
if(dir[strlen(dir) - 1] == '/')
snprintf(path, sizeof path, "%sresult.html", dir);
else
snprintf(path, sizeof path, "%s/result.html", dir);
out = fopen(path, "w"); //< Open <path>/result.html for writing
snprintf(path, sizeof path, "%s%s", dir, ofile);
out = fopen(path, "w"); //< Open for writing
if(out == NULL) {
if(dir[strlen(dir) - 1] == '/')
(void) fprintf(stderr, "ERROR: Could not open %sresult.html for writing.\n", dir);
else
(void) fprintf(stderr, "ERROR: Could not open %s/result.html for writing.\n", dir);
(void) fprintf(stderr, "ERROR: Could not open %s%s for writing.\n", dir, ofile);
exit(EXIT_FAILURE);
} else {
fprintf(out, "%s", body);
fwrite(body, 1, strlen(body), out);
fclose(out);
if(dir[strlen(dir) - 1] == '/')
printf("File %sresult.html successfully written.\n", dir);
else
printf("File %s/result.html successfully written.\n", dir);
printf("File %s%s successfully written.\n", dir, ofile);
}
}
}