Esempio n. 1
1
char *_hscan(int client, const char *reqStr, const char *msg, const char *inputstr)
{
    char *qpath = getQueryPath(reqStr);
    char *qparam = getQueryParam(reqStr, "q");
    FDPRINTF(client, OTAGA(form, action = "%s")
             "%s%s" STAG(input, type = "submit") CTAG(form), qpath, msg, inputstr);
    free(qpath);
    return qparam;
}
Esempio n. 2
0
bool route(Request request, const char * path) {
	char * queryPath = getQueryPath(request.reqStr);
	if (strcmp(queryPath,path)==0) {
		free(queryPath);
		return true;
	} else {
		free(queryPath);
		return false;
	}
}
Esempio n. 3
0
char *_hscanIfEmpty(int client, const char *reqStr, const char *msg, const char *inputstr)
{
    char *qpath = getQueryPath(reqStr);
    char *qparam = getQueryParam(reqStr, "q");
    if (strcmp(qparam, UNDEFINED) == 0) {
        FDPRINTF(client, OTAGA(form, action = "%s")
                 "%s%s" STAG(input, type = "submit") CTAG(form), qpath, msg, inputstr);
    }
    free(qpath);
    return qparam;
}
Esempio n. 4
0
bool routef(Request request, const char * path, void (* function)(int,char *, char*)) {
	char * queryPath = getQueryPath(request.reqStr);
	if (strcmp(queryPath,path)==0) {
		free(queryPath);
		function(request.client,request.reqStr,request.method);
		return true;
	} else {
		free(queryPath);
		return false;
	}
}
Esempio n. 5
0
bool routeh(Request request, const char * path)
{
	char * queryPath = getQueryPath(request.reqStr);
	if (strcmp(queryPath,path)==0) {
		free(queryPath);
		writeStandardHeaders(request.client);
		return true;
	} else {
		free(queryPath);
		return false;
	}
}
Esempio n. 6
0
bool routeh(Request request, const char * path) {
	char * queryPath = getQueryPath(request.reqStr);
	if (strcmp(queryPath,path)==0) {
		free(queryPath);
		char ** headers = sendAndReceiveHeaders(request.client);
		if (headers)
			freeHeaders(headers);
		return true;
	} else {
		free(queryPath);
		return false;
	}
}
Esempio n. 7
0
bool routefh(Request request, const char * path, void (* function)(int,char *, char*)) {
	char * queryPath = getQueryPath(request.reqStr);
	if (strcmp(queryPath,path)==0) {
		free(queryPath);
		char ** headers = sendAndReceiveHeaders(request.client);
		function(request.client,request.reqStr,request.method);
		if (headers)
			freeHeaders(headers);
		return true;
	} else {
		free(queryPath);
		return false;
	}
}
Esempio n. 8
0
bool nope_route(Request request, const char * path, void (* function)(Request),bool send_headers)
{
	char * queryPath = getQueryPath(request.reqStr);
	if (strcmp(queryPath,path)==0) {
		free(queryPath);
		if (send_headers)
			writeStandardHeaders(request.client);
		if (function!=NULL)
			function(request);
		return true;
	} else {
		free(queryPath);
		return false;
	}
}
Esempio n. 9
0
void server(Request * request, Response * response) {
	// open sqlite3 database "URL_Records"
	rc = sqlite3_open("URL_Records.db", &db);
	// if db exist it will open otherwise create a db
	if (rc) {
		fprintf(stderr, "sqlite db connectivity error.");
		exit(0);
	}
	//query to create "Records" table if not exists.
	sql = "CREATE TABLE if not exists Records( "
			"ID INTEGER PRIMARY KEY  AUTOINCREMENT,"
			"Hash CHAR(5)  NOT NULL,URL TEXT NOT NULL);";

	rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
	//Execute the sql statement.
	if (rc != SQLITE_OK) {
		fprintf(stderr, "SQL table creating error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}

	printf("%s %s\n", request->method, request->reqStr);

	if (routeRequest(request, response, "/shorten", form)
			|| routeRequest(request, response, "/", form)) {
		//Request to shorten the URL
	} else {
		response->apiFlags |= API_FLAGS_DONT_SET_HEADER_BEFORE_SENDING;
		char * queryPath = getQueryPath(request);
		char longUrl[4096];
		getLongUrl(queryPath, longUrl);
		STATIC_SEND(response->fd, "HTTP/1.1 301 Moved Permanently\r\n");
		resPrintf(response, "Location: %s\r\n", longUrl);
		STATIC_SEND(response->fd, "\r\n");
		free(queryPath);
	}
	sqlite3_close(db);

}