[feat] add server and get code (not yet finished)

This commit is contained in:
Julian Sparber
2017-07-31 14:30:22 +01:00
parent 7f243db0a5
commit 3cdafec827
7 changed files with 79 additions and 78 deletions

View File

@@ -1,10 +1,10 @@
CC ?= gcc CC ?= gcc
PKGCONFIG = $(shell which pkg-config) PKGCONFIG = $(shell which pkg-config)
CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0 avahi-client) CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0 avahi-client libsoup-2.4)
LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0 avahi-client) LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0 avahi-client libsoup-2.4)
GLIB_COMPILE_RESOURCES = $(shell $(PKGCONFIG) --variable=glib_compile_resources gio-2.0) GLIB_COMPILE_RESOURCES = $(shell $(PKGCONFIG) --variable=glib_compile_resources gio-2.0)
SRC = teleportapp.c teleportappwin.c teleportpeer.c main.c browser.c publish.c SRC = teleportapp.c teleportappwin.c teleportpeer.c main.c browser.c publish.c server.c get.c
BUILT_SRC = resources.c BUILT_SRC = resources.c
OBJS = $(BUILT_SRC:.c=.o) $(SRC:.c=.o) OBJS = $(BUILT_SRC:.c=.o) $(SRC:.c=.o)

View File

@@ -69,6 +69,7 @@ get (char *url, const gchar *output_file_path)
g_object_ref (msg); g_object_ref (msg);
soup_session_queue_message (session, msg, finished, loop); soup_session_queue_message (session, msg, finished, loop);
// soup_session_queue_message (session, msg, finished, NULL);
g_main_loop_run (loop); g_main_loop_run (loop);
name = soup_message_get_uri (msg)->path; name = soup_message_get_uri (msg)->path;
@@ -79,7 +80,8 @@ get (char *url, const gchar *output_file_path)
if (SOUP_STATUS_IS_REDIRECTION (msg->status_code)) { if (SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
g_print ("%s: %d %s\n", name, msg->status_code, msg->reason_phrase); g_print ("%s: %d %s\n", name, msg->status_code, msg->reason_phrase);
} else if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) { } else if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
output_file = fopen (concat(output_file_path, name), "w"); g_print ("###########Name: %s\n", concat(output_file_path, name));
output_file = fopen (concat(output_file_path, "name"), "w");
if (!output_file) if (!output_file)
g_printerr ("Error trying to create file %s.\n", output_file_path); g_printerr ("Error trying to create file %s.\n", output_file_path);
@@ -98,9 +100,26 @@ get (char *url, const gchar *output_file_path)
return 0; return 0;
} }
int main () //int do_get_request (char * url)
int do_client_notify (char * url)
{ {
//url and absoulte path to download dir //url and absoulte path to download dir
//get ("http://juliansparber.com/index.html", "./test_download");
get (g_strdup(url),"./test_download");
return 0;
}
int do_downloading (char * url)
{
g_print("Downloading url %s\n", url);
//get (g_strdup(url),"./test_download");
get ("http://juliansparber.com/index.html", "./test_download"); get ("http://juliansparber.com/index.html", "./test_download");
return 0; return 0;
} }
/*int main () {
get ("http://juliansparber.com/index.html", "./test_download");
return 0;
}
*/

8
src/get.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef __GET_H
#define __GET_H
extern int do_downloading(char *);
extern int do_client_notify(char *);
#endif /* __GET_H */

View File

@@ -12,6 +12,12 @@
#include <libsoup/soup.h> #include <libsoup/soup.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include "get.h"
static int port;
static SoupServer *server;
static const char *tls_cert_file, *tls_key_file;
static int static int
compare_strings (gconstpointer a, gconstpointer b) compare_strings (gconstpointer a, gconstpointer b)
{ {
@@ -223,7 +229,11 @@ do_get_response_json (SoupServer *server, SoupMessage *msg, const char *path)
soup_message_set_status (msg, SOUP_STATUS_OK); soup_message_set_status (msg, SOUP_STATUS_OK);
} }
static void handle_incoming_file(const char * hash, const char * name, const char * size, const char * origin) {
//If the user accepts the file
do_downloading(g_strdup_printf("http://%s:%d/transfer/%s", origin, port, hash));
g_print("Got a new file form %s with size:%s with title: %s\n", origin, size, name);
}
static void static void
server_callback (SoupServer *server, SoupMessage *msg, server_callback (SoupServer *server, SoupMessage *msg,
@@ -258,17 +268,17 @@ server_callback (SoupServer *server, SoupMessage *msg,
if (token != NULL && size != NULL && file_name != NULL) { if (token != NULL && size != NULL && file_name != NULL) {
g_print("Token: %s, Size: %s, Name: %s\n", token, size, file_name); g_print("Token: %s, Size: %s, Name: %s\n", token, size, file_name);
response = g_string_new("{\"error\": false, \"message\": \"Success\"}"); response = g_string_new("{\"error\": false, \"message\": \"Success\"}");
handle_incoming_file(token, file_name, size, "localhost");
} }
else else
response = g_string_new("{\"error\": true, \"message\": \"query malformed\"}"); response = g_string_new("{\"error\": true, \"message\": \"query malformed\"}");
} }
else { else {
g_print("#####Helloo crash"); g_print("No query passed");
response = g_string_new("{\"error\": true, \"message\": \"No query passed\"}"); response = g_string_new("{\"error\": true, \"message\": \"No query passed\"}");
} }
} }
//file_path = g_strdup_printf (".%s", path);
if (g_strcmp0(path, "/") == 0) { if (g_strcmp0(path, "/") == 0) {
} }
@@ -286,8 +296,6 @@ server_callback (SoupServer *server, SoupMessage *msg,
//do_get_response_json (server, msg, "hello world"); //do_get_response_json (server, msg, "hello world");
} }
//else if (msg->method == SOUP_METHOD_PUT)
// do_put (server, msg, file_path);
else else
soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED); soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
@@ -305,78 +313,32 @@ quit (int sig)
exit (0); exit (0);
} }
static int port;
static const char *tls_cert_file, *tls_key_file;
/*static GOptionEntry entries[] = { int addRouteToServer(char *name, char *file_to_send, char *destination) {
{ "cert-file", 'c', 0, soup_server_add_handler (server, g_strdup_printf("/transfer/%s", name),
G_OPTION_ARG_STRING, &tls_cert_file, server_callback, g_strdup(file_to_send), NULL);
"Use FILE as the TLS certificate file", "FILE" }, //send notification of avabile file to the client
{ "key-file", 'k', 0, //For getting file size
G_OPTION_ARG_STRING, &tls_key_file, //https://developer.gnome.org/gio/stable/GFile.html#g-file-query-info
"Use FILE as the TLS private key file", "FILE" }, do_client_notify(g_strdup_printf("http://%s:%d/?token=%s&size=0&name=%s\n", destination, port, name, file_to_send));
{ "port", 'p', 0, return 0;
G_OPTION_ARG_INT, &port, }
"Port to listen on", NULL },
{ NULL }
};
*/
int createServer(char *file_to_send) int run_http_server() {
{ //GMainLoop *loop;
GMainLoop *loop;
SoupServer *server;
GSList *uris, *u; GSList *uris, *u;
char *str; char *str;
GTlsCertificate *cert; GTlsCertificate *cert;
GError *error = NULL; GError *error = NULL;
/*opts = g_option_context_new (NULL);
g_option_context_add_main_entries (opts, entries, NULL);
if (!g_option_context_parse (opts, &argc, &argv, &error)) {
g_printerr ("Could not parse arguments: %s\n",
error->message);
g_printerr ("%s",
g_option_context_get_help (opts, TRUE, NULL));
exit (1);
}
if (argc != 1) {
g_printerr ("%s",
g_option_context_get_help (opts, TRUE, NULL));
exit (1);
}
g_option_context_free (opts);
*/
//signal (SIGINT, quit);
port = 3000; port = 3000;
/*if (tls_cert_file && tls_key_file) {
cert = g_tls_certificate_new_from_files (tls_cert_file, tls_key_file, &error);
if (error) {
g_printerr ("Unable to create server: %s\n", error->message);
exit (1);
}
server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "teleport-httpd ",
SOUP_SERVER_TLS_CERTIFICATE, cert,
NULL);
g_object_unref (cert);
soup_server_listen_all (server, port, SOUP_SERVER_LISTEN_HTTPS, &error);
} else {
*/
server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "teleport-httpd ", server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "teleport-httpd ",
NULL); NULL);
soup_server_listen_all (server, port, 0, &error); soup_server_listen_all (server, port, 0, &error);
//}
soup_server_add_handler (server, NULL, soup_server_add_handler (server, NULL,
server_callback, NULL, NULL); server_callback, NULL, NULL);
soup_server_add_handler (server, "/transfer/file1",
server_callback, file_to_send, NULL);
uris = soup_server_get_uris (server); uris = soup_server_get_uris (server);
for (u = uris; u; u = u->next) { for (u = uris; u; u = u->next) {
str = soup_uri_to_string (u->data, FALSE); str = soup_uri_to_string (u->data, FALSE);
@@ -388,17 +350,17 @@ int createServer(char *file_to_send)
g_print ("\nWaiting for requests...\n"); g_print ("\nWaiting for requests...\n");
loop = g_main_loop_new (NULL, TRUE); //loop = g_main_loop_new (NULL, TRUE);
g_main_loop_run (loop); //g_main_loop_run (loop);
return 0; return 0;
} }
int
/* int
main (int argc, char **argv) main (int argc, char **argv)
{ {
//GOptionContext *opts;
char *file_to_send = "/home/julian/teleport/docs/flow-diagram.svg"; char *file_to_send = "/home/julian/teleport/docs/flow-diagram.svg";
createServer(file_to_send); createServer(file_to_send);
return 0; return 0;
} }
*/

8
src/server.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef __SERVER_H
#define __SERVER_H
extern int run_http_server();
extern int addRouteToServer();
#endif /* __SERVER_H */

View File

@@ -5,6 +5,7 @@
#include "teleportappwin.h" #include "teleportappwin.h"
#include "browser.h" #include "browser.h"
#include "publish.h" #include "publish.h"
#include "server.h"
static TeleportAppWindow *win; static TeleportAppWindow *win;
@@ -56,6 +57,7 @@ teleport_app_activate (GApplication *app)
g_print("Data: %s\n", teleport_peer_get_ip(peerList, 0, NULL)); g_print("Data: %s\n", teleport_peer_get_ip(peerList, 0, NULL));
g_print("Data: %d\n", teleport_peer_get_port(peerList, 0, NULL)); g_print("Data: %d\n", teleport_peer_get_port(peerList, 0, NULL));
*/ */
run_http_server();
run_avahi_publish_service("Angela's (self)"); run_avahi_publish_service("Angela's (self)");
run_avahi_service(peerList); run_avahi_service(peerList);
GNotification *notification = g_notification_new ("Hello world!"); GNotification *notification = g_notification_new ("Hello world!");

View File

@@ -2,6 +2,7 @@
#include "teleportapp.h" #include "teleportapp.h"
#include "teleportappwin.h" #include "teleportappwin.h"
#include "server.h"
GtkWidget* find_child(GtkWidget* , const gchar* ); GtkWidget* find_child(GtkWidget* , const gchar* );
@@ -62,6 +63,7 @@ open_file_picker(GtkButton * btn, GtkWidget* win) {
GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog); GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
filename = gtk_file_chooser_get_filename (chooser); filename = gtk_file_chooser_get_filename (chooser);
g_print("Choosen file is %s\n", filename); g_print("Choosen file is %s\n", filename);
addRouteToServer(g_compute_checksum_for_string (G_CHECKSUM_SHA256, filename, -1), filename, "localhost");
//open_file (filename); //open_file (filename);
g_free (filename); g_free (filename);
} }