This repository has been archived on 2021-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
unix/http/client.c

85 lines
2.3 KiB
C
Raw Normal View History

#define _GNU_SOURCE ///< in order for optarg to be defined
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "shared/http.h"
int main(int argc, char *argv[]) {
check_opts_number(argc);
int port = 80;
char *ofile = NULL;
char *dir = NULL;
char *url = NULL;
int c;
/// Process the options
while((c = getopt(argc, argv, "p:o:d:h")) != -1) {
switch(c) {
case 'p':
port = atoi(optarg);
break;
case 'o':
ofile = optarg;
break;
case 'd':
dir = optarg;
break;
case 'h':
printf("Command usage:\n");
print_usage();
exit(EXIT_SUCCESS);
break;
case '?':
print_usage();
exit(EXIT_FAILURE);
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");
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];
}
const char *full_url = strstr(url, "http://") + 7; //< the full url starts after http://
const char *path = strstr(full_url, "/");
}
static void print_usage(void) {
printf("client [-p PORT] [ -o FILE | -d DIR ] URL\n");
}
/**
* @brief Function to check the number of arguments
* @details Checks the number of arguments supplied to the command line.
* If the arguments are less than the required or more than possible, it exits
* @param argc Number of arguments obtained from the command line
* @return none
*
**/
static void check_opts_number(int argc) {
if(argc <= 1) {
fprintf(stderr, "At least one argument expected.\n");
print_usage();
exit(EXIT_FAILURE);
} else if(argc > 6) {
fprintf(stderr, "Too many arguments supplied.\n");
print_usage();
exit(EXIT_FAILURE);
}
}