Refacto
This commit is contained in:
parent
ac30a78245
commit
597d6ec73a
10
Makefile
10
Makefile
|
@ -2,6 +2,8 @@ CC=gcc
|
||||||
|
|
||||||
all: gemcurl
|
all: gemcurl
|
||||||
|
|
||||||
|
debug: gemcurl_debug
|
||||||
|
|
||||||
gemlib.o: src/gemlib.c src/gemlib.h
|
gemlib.o: src/gemlib.c src/gemlib.h
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
$(CC) -o build/gemlib.o -c src/gemlib.c
|
$(CC) -o build/gemlib.o -c src/gemlib.c
|
||||||
|
@ -10,6 +12,14 @@ gemcurl.o: src/gemlib.h src/gemcurl.c
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
$(CC) -o build/gemcurl.o -c src/gemcurl.c
|
$(CC) -o build/gemcurl.o -c src/gemcurl.c
|
||||||
|
|
||||||
|
gemcurl_debug.o: src/gemlib.h src/gemcurl.c
|
||||||
|
mkdir -p build
|
||||||
|
$(CC) -o build/gemcurl_debug.o -c src/gemcurl.c -DDEBUG
|
||||||
|
|
||||||
gemcurl: gemcurl.o gemlib.o
|
gemcurl: gemcurl.o gemlib.o
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
$(CC) -o build/gemcurl build/gemcurl.o build/gemlib.o -lcrypto -lssl
|
$(CC) -o build/gemcurl build/gemcurl.o build/gemlib.o -lcrypto -lssl
|
||||||
|
|
||||||
|
gemcurl_debug: gemcurl_debug.o gemlib.o
|
||||||
|
mkdir -p build
|
||||||
|
$(CC) -o build/gemcurl_debug build/gemcurl_debug.o build/gemlib.o -lcrypto -lssl
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
#include "gemlib.h"
|
#include "gemlib.h"
|
||||||
|
|
||||||
#define VERSION_MAJ 0
|
#define VERSION_MAJ 0
|
||||||
#define VERSION_MIN 0
|
#define VERSION_MIN 1
|
||||||
#define VERSION_REV 0
|
#define VERSION_REV 0
|
||||||
#define NAME "gemcurl"
|
#define NAME "gemcurl"
|
||||||
|
|
||||||
|
@ -17,6 +17,8 @@ void printHelp(void);
|
||||||
|
|
||||||
int main (int argc, char **argv)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
GEM_Request *request;
|
||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
{
|
{
|
||||||
printUsage(argv[0]);
|
printUsage(argv[0]);
|
||||||
|
@ -55,9 +57,17 @@ int main (int argc, char **argv)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getGem(argv[2], stdout) != 0)
|
request = GEM_new_request(argv[2]);
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
GEM_print_request_info(request);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (GEM_send_request(request, stdout) != 0)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
|
GEM_free_request(request);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
66
src/gemlib.c
66
src/gemlib.c
|
@ -9,15 +9,21 @@
|
||||||
|
|
||||||
#include "gemlib.h"
|
#include "gemlib.h"
|
||||||
|
|
||||||
Request *examineRequest(const char *request_content)
|
void GEM_init (void)
|
||||||
|
{
|
||||||
|
SSL_load_error_strings();
|
||||||
|
SSL_library_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
GEM_Request *GEM_new_request(const char *request_content)
|
||||||
{
|
{
|
||||||
int i = 0, len = 0, len_req = strlen(request_content);
|
int i = 0, len = 0, len_req = strlen(request_content);
|
||||||
Request *request = malloc(sizeof(Request));
|
GEM_Request *request = malloc(sizeof(GEM_Request));
|
||||||
|
|
||||||
if (request == NULL)
|
if (request == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
memset(request, 0, sizeof(Request));
|
memset(request, 0, sizeof(GEM_Request));
|
||||||
|
|
||||||
strcpy(request->request_content, request_content);
|
strcpy(request->request_content, request_content);
|
||||||
strcat(request->request_content, "\r\n");
|
strcat(request->request_content, "\r\n");
|
||||||
|
@ -72,19 +78,25 @@ Request *examineRequest(const char *request_content)
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
void requestTheServer(Request *request, FILE *answerFile)
|
void GEM_print_request_info (GEM_Request *request)
|
||||||
|
{
|
||||||
|
printf("Request content : %s\nRequest scheme : %s\nRequest hostname : %s\nRequest path : %s\n", request->request_content, request->scheme, request->hostname, request->path);
|
||||||
|
}
|
||||||
|
|
||||||
|
int GEM_send_request (GEM_Request *request, FILE *answerFile)
|
||||||
{
|
{
|
||||||
const SSL_METHOD *method = TLS_client_method();
|
const SSL_METHOD *method = TLS_client_method();
|
||||||
SSL *ssl = NULL;
|
SSL *ssl = NULL;
|
||||||
SSL_CTX *ctx = NULL;
|
SSL_CTX *ctx = NULL;
|
||||||
BIO *bio = NULL;
|
BIO *bio = NULL;
|
||||||
int n;
|
int n;
|
||||||
|
char buffer[1024];
|
||||||
|
|
||||||
if (method == NULL)
|
if (method == NULL)
|
||||||
{
|
{
|
||||||
perror("Method");
|
perror("Method");
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
exit(1);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx = SSL_CTX_new(method);
|
ctx = SSL_CTX_new(method);
|
||||||
|
@ -93,7 +105,7 @@ void requestTheServer(Request *request, FILE *answerFile)
|
||||||
{
|
{
|
||||||
perror("ctx");
|
perror("ctx");
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
exit(2);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bio = BIO_new_ssl_connect(ctx);
|
bio = BIO_new_ssl_connect(ctx);
|
||||||
|
@ -103,7 +115,7 @@ void requestTheServer(Request *request, FILE *answerFile)
|
||||||
perror("bio");
|
perror("bio");
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
SSL_CTX_free(ctx);
|
SSL_CTX_free(ctx);
|
||||||
exit(3);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
BIO_get_ssl(bio, &ssl);
|
BIO_get_ssl(bio, &ssl);
|
||||||
|
@ -116,44 +128,26 @@ void requestTheServer(Request *request, FILE *answerFile)
|
||||||
SSL_CTX_free(ctx);
|
SSL_CTX_free(ctx);
|
||||||
perror("bio connect");
|
perror("bio connect");
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
exit(4);
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
BIO_puts(bio, request->request_content);
|
BIO_puts(bio, request->request_content);
|
||||||
|
|
||||||
while (1)
|
n = BIO_read(bio, buffer, 1023);
|
||||||
|
|
||||||
|
while (n > 0)
|
||||||
{
|
{
|
||||||
memset(request->answer_content, 0, sizeof(request->answer_content));
|
fwrite(buffer, sizeof(char), n, answerFile);
|
||||||
n = BIO_read(bio, request->answer_content, 1024);
|
n = BIO_read(bio, buffer, 1024);
|
||||||
if (n <= 0) break;
|
|
||||||
fprintf(answerFile, "%s", request->answer_content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BIO_free_all(bio);
|
BIO_free_all(bio);
|
||||||
SSL_CTX_free(ctx);
|
SSL_CTX_free(ctx);
|
||||||
}
|
|
||||||
|
|
||||||
void initSSL (void)
|
|
||||||
{
|
|
||||||
SSL_load_error_strings();
|
|
||||||
SSL_library_init();
|
|
||||||
}
|
|
||||||
|
|
||||||
int getGem (const char *URL, FILE *answerFile)
|
|
||||||
{
|
|
||||||
Request *request = examineRequest(URL);
|
|
||||||
|
|
||||||
initSSL();
|
|
||||||
|
|
||||||
if (request == NULL)
|
|
||||||
{
|
|
||||||
printf("error during url parsing\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
requestTheServer(request, answerFile);
|
|
||||||
|
|
||||||
free(request);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GEM_free_request (GEM_Request *request)
|
||||||
|
{
|
||||||
|
free(request);
|
||||||
|
}
|
||||||
|
|
15
src/gemlib.h
15
src/gemlib.h
|
@ -6,17 +6,16 @@ typedef struct _gemini_request {
|
||||||
char hostname[256];
|
char hostname[256];
|
||||||
char scheme[64];
|
char scheme[64];
|
||||||
char path[1024];
|
char path[1024];
|
||||||
char answer_code[2];
|
} GEM_Request;
|
||||||
char answer_meta[2];
|
|
||||||
char answer_content[8192];
|
|
||||||
} Request;
|
|
||||||
|
|
||||||
Request *examineRequest(const char *request_content);
|
void GEM_init (void);
|
||||||
|
|
||||||
void requestTheServer(Request *request, FILE *answerFile);
|
GEM_Request *GEM_new_request(const char *request_content);
|
||||||
|
|
||||||
void initSSL (void);
|
void GEM_print_request_info (GEM_Request *request);
|
||||||
|
|
||||||
int getGem (const char *URL, FILE *answerFile);
|
int GEM_send_request (GEM_Request *request, FILE *answerFile);
|
||||||
|
|
||||||
|
void GEM_free_request (GEM_Request *request);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue