Skip to content

Commit

Permalink
serious code modification
Browse files Browse the repository at this point in the history
  • Loading branch information
rishithaminol committed Apr 24, 2018
1 parent 659850c commit 3134b57
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*.o
cmarketcap
*.db-journal
*/doxygen/*

test/url_token_test
test/mysql_api_test
test/http_test
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS=-Wall -g $(shell pkg-config --cflags jansson libcurl) $(shell mysql_config --cflags) -DCM_DEBUG_
CFLAGS=-Wall -g -I./http-parser $(shell pkg-config --cflags jansson libcurl) $(shell mysql_config --cflags) -DCM_DEBUG_
LDFLAGS=$(shell pkg-config --libs jansson libcurl) $(shell mysql_config --libs) -lpthread

CC=gcc
Expand All @@ -7,7 +7,7 @@ TARGETS=cmarketcap
DOXYGEN=doxygen


cmarketcap: mysql_api.o signal_handler.o timer.o httpd.o cm_debug.o json_parser.o cmarketcap.c
cmarketcap: mysql_api.o signal_handler.o timer.o httpd.o cm_debug.o json_parser.o cmarketcap.c http-parser/http_parser.o
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

mysql_api.o: mysql_api.c mysql_api.h
Expand Down
2 changes: 1 addition & 1 deletion cmarketcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void *__cb_update_database(void *arg)
struct __cb_args *__cb_arg;
__cb_arg = (struct __cb_args *)arg;

pthread_detach(pthread_self());
pthread_detach(pthread_self()); /* behaves as a self controlled thread */

while (1) {
DEBUG_MSG("Starting to count..\n");
Expand Down
6 changes: 6 additions & 0 deletions cmarketcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

#include <pthread.h>

/**
* @brief Remember the file descriptor of openned socket.
*
* This global structure is used for close the connection
* in unexpected exits.
*/
struct global_data_handle {
int httpd_sockfd;
};
Expand Down
31 changes: 21 additions & 10 deletions httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "mysql_api.h"
#include "httpd.h"
#include "cmarketcap.h"
#include "http_parser.h"

#define RD_BUFF_MAX 1024
#define CLIENT_MAX 10
Expand Down Expand Up @@ -45,7 +46,7 @@ static void parse_http_header(char *buff, struct myhttp_header *header)
char *token;
char *line = NULL;

/* fetch the first line. like 'GET /path HTTP/1.1' */
/* fetch the first line. like 'GET /home/path?coinid=bitcoin&start=min_5 HTTP/1.1' */
line = strtok_r(line, "\n", &buff);
DEBUG_MSG("request line = \"%s\"\n", line);

Expand Down Expand Up @@ -174,34 +175,42 @@ static void append_uri_entry(struct uri_base *ub, struct uri_entry *ue)
ub->last->index = (size_t)ub->entry_count++;
}

/* @brief delimeter charachter is '&'
/**
* @brief delimeter charachter is '&'
*
* '/any/path?hello=world&rishitha=minol' breaks this string using '&'
* and pass 'key=value' like strings to 'mk_uri_entry()'
*
* @param uri This string gets modified.
* @return returns 'NULL' on error
*/
struct uri_base *tokenize_uri(const char *uri)
struct uri_base *tokenize_uri(const char *url)
{
char *x = NULL;
char *y;
char uri_[MAX_URI_SIZE];
struct uri_base *uri_base;
struct uri_entry *t;

struct uri_base *uri_base = init_uri_base();
struct http_parser_url u; /*!< http_parser.h - 4th field is the url options */
http_parser_url_init(&u);
size_t len = strlen(url);
http_parser_parse_url(url, len, 0, &u);

if ((u.field_set & (1 << 4)) == 0) /* no data */
return NULL;

strncpy(uri_, url + u.field_data[4].off, u.field_data[4].len);//MAX_URI_SIZE);

strncpy(uri_, uri, MAX_URI_SIZE);
x = uri_; /*!< start of the url options 'hello=world&rishitha=minol' */

x = strchr(uri_, '?'); /**! start of the url options */
/* 'hello=world&rishitha=minol' */
if (x == NULL) {
CM_ERROR("path string\n");
free(uri_base);
return NULL;
}

x++;
uri_base = init_uri_base();

while (x != NULL) {
y = x;
x = strchr(x, '&');
Expand Down Expand Up @@ -271,14 +280,16 @@ static void send_json_response(int sockfd, struct myhttp_header *header, MYSQL *
strcpy(code, "200");
send_header(sockfd, code, "application/json");


/* example url sets
*
* /home/path?rank=full
* /home/path?coinid=bitcoin&start=min_5&limit=min_30
*/
struct uri_base *tokens;
struct uri_entry *te_entry;
tokens = tokenize_uri(header->url);
DEBUG_MSG("%s\n", header->url);
tokens = tokenize_uri(header->url); /* what happens if empty string comes here */
if (tokens == NULL) {
CM_ERROR("tokenization malfunction\n");
goto error__;
Expand Down
2 changes: 1 addition & 1 deletion httpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ extern void free_uri_base(struct uri_base *ub);
extern size_t num_of_clients();
extern void init_httpd_mutexes();

#endif
#endif
6 changes: 4 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LDFLAGS=$(shell pkg-config --libs jansson libcurl) -lpthread

CC=gcc

TARGETS=signal_handler_test http_parser url_token_test mysql_api_test
TARGETS=signal_handler_test http_parser url_token_test mysql_api_test http_test

MYSQL_CFLAGS=$(shell mysql_config --cflags) -DCM_DEBUG_ -I$(PREV_DIR)
MYSQL_LDFLAGS=$(shell mysql_config --libs)
Expand All @@ -17,6 +17,9 @@ url_token_test: httpd.o cm_debug.o sql_api.o cmarketcap.o json_parser.o timer.o
mysql_api_test: cm_debug.o mysql_api_test.c
$(CC) $(MYSQL_CFLAGS) $^ -o $@ $(MYSQL_LDFLAGS)

http_test: ../http-parser/http_parser.o http_test.c
$(CC) $(CFLAGS) -I../http-parser/ $^ -o $@ $(LDFLAGS)

httpd.o: $(PREV_DIR)/httpd.c
$(CC) $(CFLAGS) -c $^ -o ./$@

Expand Down Expand Up @@ -45,4 +48,3 @@ signal_handler.o: $(PREV_DIR)/signal_handler.c

clean:
rm -rf *.o $(TARGETS)

0 comments on commit 3134b57

Please sign in to comment.