[feat] add server and get code (not yet finished)
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
CC ?= gcc
|
||||
PKGCONFIG = $(shell which pkg-config)
|
||||
CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0 avahi-client)
|
||||
LIBS = $(shell $(PKGCONFIG) --libs 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 libsoup-2.4)
|
||||
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
|
||||
|
||||
OBJS = $(BUILT_SRC:.c=.o) $(SRC:.c=.o)
|
||||
|
||||
23
src/get.c
23
src/get.c
@@ -69,6 +69,7 @@ get (char *url, const gchar *output_file_path)
|
||||
|
||||
g_object_ref (msg);
|
||||
soup_session_queue_message (session, msg, finished, loop);
|
||||
// soup_session_queue_message (session, msg, finished, NULL);
|
||||
g_main_loop_run (loop);
|
||||
|
||||
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)) {
|
||||
g_print ("%s: %d %s\n", name, msg->status_code, msg->reason_phrase);
|
||||
} 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)
|
||||
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;
|
||||
}
|
||||
|
||||
int main ()
|
||||
//int do_get_request (char * url)
|
||||
int do_client_notify (char * url)
|
||||
{
|
||||
//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");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*int main () {
|
||||
get ("http://juliansparber.com/index.html", "./test_download");
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
8
src/get.h
Normal file
8
src/get.h
Normal 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 */
|
||||
108
src/server.c
108
src/server.c
@@ -12,6 +12,12 @@
|
||||
#include <libsoup/soup.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
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
server_callback (SoupServer *server, SoupMessage *msg,
|
||||
@@ -258,17 +268,17 @@ server_callback (SoupServer *server, SoupMessage *msg,
|
||||
if (token != NULL && size != NULL && file_name != NULL) {
|
||||
g_print("Token: %s, Size: %s, Name: %s\n", token, size, file_name);
|
||||
response = g_string_new("{\"error\": false, \"message\": \"Success\"}");
|
||||
handle_incoming_file(token, file_name, size, "localhost");
|
||||
}
|
||||
else
|
||||
response = g_string_new("{\"error\": true, \"message\": \"query malformed\"}");
|
||||
}
|
||||
else {
|
||||
g_print("#####Helloo crash");
|
||||
g_print("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) {
|
||||
}
|
||||
@@ -281,13 +291,11 @@ server_callback (SoupServer *server, SoupMessage *msg,
|
||||
SOUP_MEMORY_TAKE,
|
||||
response->str, response->len);
|
||||
soup_message_set_status (msg, SOUP_STATUS_OK);
|
||||
g_print("Handle response\n");
|
||||
g_print("Handle response\n");
|
||||
}
|
||||
|
||||
|
||||
//do_get_response_json (server, msg, "hello world");
|
||||
}
|
||||
//else if (msg->method == SOUP_METHOD_PUT)
|
||||
// do_put (server, msg, file_path);
|
||||
else
|
||||
soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
|
||||
|
||||
@@ -305,78 +313,32 @@ quit (int sig)
|
||||
exit (0);
|
||||
}
|
||||
|
||||
static int port;
|
||||
static const char *tls_cert_file, *tls_key_file;
|
||||
|
||||
/*static GOptionEntry entries[] = {
|
||||
{ "cert-file", 'c', 0,
|
||||
G_OPTION_ARG_STRING, &tls_cert_file,
|
||||
"Use FILE as the TLS certificate file", "FILE" },
|
||||
{ "key-file", 'k', 0,
|
||||
G_OPTION_ARG_STRING, &tls_key_file,
|
||||
"Use FILE as the TLS private key file", "FILE" },
|
||||
{ "port", 'p', 0,
|
||||
G_OPTION_ARG_INT, &port,
|
||||
"Port to listen on", NULL },
|
||||
{ NULL }
|
||||
};
|
||||
*/
|
||||
int addRouteToServer(char *name, char *file_to_send, char *destination) {
|
||||
soup_server_add_handler (server, g_strdup_printf("/transfer/%s", name),
|
||||
server_callback, g_strdup(file_to_send), NULL);
|
||||
//send notification of avabile file to the client
|
||||
//For getting file size
|
||||
//https://developer.gnome.org/gio/stable/GFile.html#g-file-query-info
|
||||
do_client_notify(g_strdup_printf("http://%s:%d/?token=%s&size=0&name=%s\n", destination, port, name, file_to_send));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int createServer(char *file_to_send)
|
||||
{
|
||||
GMainLoop *loop;
|
||||
SoupServer *server;
|
||||
int run_http_server() {
|
||||
//GMainLoop *loop;
|
||||
GSList *uris, *u;
|
||||
char *str;
|
||||
GTlsCertificate *cert;
|
||||
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;
|
||||
|
||||
/*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 ",
|
||||
NULL);
|
||||
soup_server_listen_all (server, port, 0, &error);
|
||||
//}
|
||||
server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "teleport-httpd ",
|
||||
NULL);
|
||||
soup_server_listen_all (server, port, 0, &error);
|
||||
|
||||
soup_server_add_handler (server, NULL,
|
||||
server_callback, NULL, NULL);
|
||||
|
||||
soup_server_add_handler (server, "/transfer/file1",
|
||||
server_callback, file_to_send, NULL);
|
||||
|
||||
uris = soup_server_get_uris (server);
|
||||
for (u = uris; u; u = u->next) {
|
||||
str = soup_uri_to_string (u->data, FALSE);
|
||||
@@ -388,17 +350,17 @@ int createServer(char *file_to_send)
|
||||
|
||||
g_print ("\nWaiting for requests...\n");
|
||||
|
||||
loop = g_main_loop_new (NULL, TRUE);
|
||||
g_main_loop_run (loop);
|
||||
//loop = g_main_loop_new (NULL, TRUE);
|
||||
//g_main_loop_run (loop);
|
||||
|
||||
return 0;
|
||||
}
|
||||
int
|
||||
|
||||
/* int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
//GOptionContext *opts;
|
||||
char *file_to_send = "/home/julian/teleport/docs/flow-diagram.svg";
|
||||
createServer(file_to_send);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
8
src/server.h
Normal file
8
src/server.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef __SERVER_H
|
||||
#define __SERVER_H
|
||||
|
||||
|
||||
extern int run_http_server();
|
||||
extern int addRouteToServer();
|
||||
|
||||
#endif /* __SERVER_H */
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "teleportappwin.h"
|
||||
#include "browser.h"
|
||||
#include "publish.h"
|
||||
#include "server.h"
|
||||
|
||||
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: %d\n", teleport_peer_get_port(peerList, 0, NULL));
|
||||
*/
|
||||
run_http_server();
|
||||
run_avahi_publish_service("Angela's (self)");
|
||||
run_avahi_service(peerList);
|
||||
GNotification *notification = g_notification_new ("Hello world!");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "teleportapp.h"
|
||||
#include "teleportappwin.h"
|
||||
#include "server.h"
|
||||
|
||||
|
||||
GtkWidget* find_child(GtkWidget* , const gchar* );
|
||||
@@ -62,6 +63,7 @@ open_file_picker(GtkButton * btn, GtkWidget* win) {
|
||||
GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
|
||||
filename = gtk_file_chooser_get_filename (chooser);
|
||||
g_print("Choosen file is %s\n", filename);
|
||||
addRouteToServer(g_compute_checksum_for_string (G_CHECKSUM_SHA256, filename, -1), filename, "localhost");
|
||||
//open_file (filename);
|
||||
g_free (filename);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user