Refactor the client
This commit is contained in:
parent
712e3910ad
commit
72fb0784bf
@ -36,7 +36,9 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
char *port = "80";
|
char *port = "80";
|
||||||
char *ofile = NULL;
|
char *ofile = NULL;
|
||||||
|
int opt_f = 0;
|
||||||
char *dir = NULL;
|
char *dir = NULL;
|
||||||
|
int opt_d = 0;
|
||||||
char *url = NULL;
|
char *url = NULL;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
@ -48,44 +50,61 @@ int main(int argc, char *argv[]) {
|
|||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
ofile = optarg;
|
ofile = optarg;
|
||||||
|
opt_f = 1;
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
dir = optarg;
|
dir = optarg;
|
||||||
|
opt_d = 1;
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
printf("Command usage:\n");
|
|
||||||
print_usage();
|
print_usage();
|
||||||
exit(EXIT_SUCCESS);
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
print_usage();
|
print_usage();
|
||||||
exit(EXIT_FAILURE);
|
break;
|
||||||
default:
|
default:
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check for the case that both output file and directory are set
|
/// Check for the case that both output file and directory are set
|
||||||
if(ofile != NULL && dir != NULL) {
|
if(opt_d == 1 && opt_f == 1) {
|
||||||
printf("Either output file or directory is specified. You cannot use both.\n");
|
printf("Either output file or directory should be specified. You cannot use both.\n");
|
||||||
print_usage();
|
print_usage();
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if the required argument is there
|
/// Check if the required argument is there
|
||||||
if(argv[optind] == NULL) {
|
if(argv[optind] == NULL) {
|
||||||
fprintf(stderr, "Mandatory argument 'url' missing.\n");
|
fprintf(stderr, "Mandatory argument 'url' missing.\n");
|
||||||
print_usage();
|
print_usage();
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
} else {
|
} else {
|
||||||
url = argv[optind];
|
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 *base_url = strstr(url, "http://") + 7; //< the base url starts after http://
|
||||||
|
|
||||||
char *path = malloc(strlen(base_url) + 1);
|
char *path = malloc(strlen(base_url) + 1);
|
||||||
strcpy(path, base_url); //< Copying so that strtok() doesn't change it
|
strcpy(path, base_url); //< Copying so that strtok() doesn't change it
|
||||||
path = strstr(path, "/");
|
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
|
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
|
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) {
|
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;
|
char *body = strstr(reply, "\r\n\r\n") + 4;
|
||||||
if(ofile == NULL && dir == NULL) printf("%s", body); //< Print the body of the response
|
if(ofile == NULL && dir == NULL) printf("%s", body); //< Print the body of the response
|
||||||
else {
|
else {
|
||||||
if(ofile != NULL) {
|
FILE *out = NULL;
|
||||||
FILE *out = NULL;
|
if(ofile != NULL && dir == NULL) {
|
||||||
out = fopen(ofile, "w");
|
out = fopen(ofile, "w");
|
||||||
|
|
||||||
if(out == NULL) {
|
if(out == NULL) {
|
||||||
(void) fprintf(stderr, "ERROR: Could not open ./%s for writing.", ofile);
|
(void) fprintf(stderr, "ERROR: Could not open ./%s for writing.", ofile);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
} else {
|
} else {
|
||||||
fprintf(out, "%s", body);
|
fwrite(body, 1, strlen(body), out);
|
||||||
fclose(out);
|
fclose(out);
|
||||||
printf("File ./%s successfully written.\n", ofile);
|
printf("File ./%s successfully written.\n", ofile);
|
||||||
}
|
}
|
||||||
} else if(dir != NULL) {
|
} else if(ofile != NULL && dir != NULL) {
|
||||||
FILE *out = NULL;
|
|
||||||
|
|
||||||
char path[sizeof(dir) + 100];
|
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(out == NULL) {
|
||||||
if(dir[strlen(dir) - 1] == '/')
|
(void) fprintf(stderr, "ERROR: Could not open %s%s for writing.\n", dir, ofile);
|
||||||
(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);
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
} else {
|
} else {
|
||||||
fprintf(out, "%s", body);
|
fwrite(body, 1, strlen(body), out);
|
||||||
fclose(out);
|
fclose(out);
|
||||||
|
printf("File %s%s successfully written.\n", dir, ofile);
|
||||||
if(dir[strlen(dir) - 1] == '/')
|
|
||||||
printf("File %sresult.html successfully written.\n", dir);
|
|
||||||
else
|
|
||||||
printf("File %s/result.html successfully written.\n", dir);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user