28#include <microhttpd.h>
66static struct MHD_Daemon *
httpd = NULL;
127 {
"prefix",
"prefix"},
129 {
"suffix",
"suffix"},
130 {
"street",
"street"},
135 {
"homepage",
"homepage"},
136 {
"org",
"organization"},
137 {
"department",
"department"},
138 {
"subdepartment",
"subdepartment"},
139 {
"jobtitle",
"jobtitle"},
204 MHD_stop_daemon (
httpd);
227 unsigned MHD_LONG_LONG
timeout = 0;
291 struct MHD_Connection *connection,
295 const char *upload_data,
296 size_t *upload_data_size,
300 bool isget = (0 == strcmp (method, MHD_HTTP_METHOD_GET));
301 bool ishead = (0 == strcmp (method, MHD_HTTP_METHOD_HEAD));
302 bool isfull = (0 == strcmp (
"/submit/full", url));
303 bool issimple = (0 == strcmp (
"/submit/simple", url));
305 char *defpath = NULL;
306 const char *gpgfp = MHD_lookup_connection_value (connection,
307 MHD_GET_ARGUMENT_KIND,
309 const char *gnsnick = MHD_lookup_connection_value (connection,
310 MHD_GET_ARGUMENT_KIND,
312 const char *gnskey = MHD_lookup_connection_value (connection,
313 MHD_GET_ARGUMENT_KIND,
315 const char *qrpng = MHD_lookup_connection_value (connection,
316 MHD_GET_ARGUMENT_KIND,
322 (void) upload_data_size;
324 if (! isget && ! ishead)
326 return MHD_queue_response (connection,
335 return MHD_queue_response (connection,
349 if (0 == strcmp (
"/", url))
351 return MHD_queue_response (connection,
356 if (0 == strcmp (
"/full", url))
358 return MHD_queue_response (connection,
364 if (! isfull && ! issimple)
366 return MHD_queue_response (connection,
375 return MHD_queue_response (connection,
384 return MHD_queue_response (connection,
392 FILE *deffile = fopen (defpath,
"w");
399 return MHD_queue_response (connection,
406 for (
size_t i = 0; NULL!=
pmap[i].
name; ++i)
408 const char *
value = MHD_lookup_connection_value (connection,
409 MHD_GET_ARGUMENT_KIND,
419 size_t len = strlen (gpgfp);
423 "\\def\\gpglineone{%s}\n\\def\\gpglinetwo{%s}\n",
431 "\\def\\gns{%s/%s}\n",
433 (NULL == gnsnick) ?
"" : gnsnick);
438 char *command = NULL;
441 "cd %s; cp %s gns-bcd.tex; "
442 "pdflatex %s gns-bcd.tex >/dev/null 2>&1",
446 (NULL == qrpng) ?
"" :
"-shell-escape");
448 ret = system (command);
450 if (WIFSIGNALED (
ret) || 0 != WEXITSTATUS (
ret))
461 (NULL == qrpng) ?
"gns-bcd.pdf" :
"gns-bcd.png");
464 struct MHD_Response *pdfrs;
466 int pdf = open (defpath, O_RDONLY);
473 return MHD_queue_response (connection,
482 pdfrs = MHD_create_response_from_fd ((
size_t) statret.st_size, pdf);
489 return MHD_queue_response (connection,
495 MHD_HTTP_HEADER_CONTENT_TYPE,
500 MHD_add_response_header (pdfrs,
501 MHD_HTTP_HEADER_CONTENT_DISPOSITION,
503 "attachment; filename=\"gns-business-card.pdf\""
505 "attachment; filename=\"gns-qr-code.png\""));
506 r = MHD_queue_response (connection,
MHD_HTTP_OK, pdfrs);
508 MHD_destroy_response (pdfrs);
526 char *fullname = NULL;
589 _ (
"Invalid port number %u\n"),
610 "gns-bcd-simple.tex");
632 _ (
"Unable to set up the daemon\n"));
638 int flags = MHD_USE_DUAL_STACK | MHD_USE_DEBUG | MHD_ALLOW_SUSPEND_RESUME;
641 httpd = MHD_start_daemon (flags,
645 MHD_OPTION_CONNECTION_LIMIT, 512,
646 MHD_OPTION_PER_IP_CONNECTION_LIMIT, 2,
647 MHD_OPTION_CONNECTION_TIMEOUT, 60,
648 MHD_OPTION_CONNECTION_MEMORY_LIMIT, 16 * 1024,
650 flags = MHD_USE_DEBUG;
651 }
while (NULL ==
httpd && flags != MHD_USE_DEBUG);
656 _ (
"Failed to start HTTP server\n"));
673main (
int argc,
char *
const *argv)
680 gettext_noop (
"Run HTTP server on port PORT (default is 8888)"),
690 _ (
"GNUnet HTTP server to create business cards")
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
struct GNUNET_GETOPT_CommandLineOption options[]
#define gettext_noop(String)
static int ret
Final status code.
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
static char * tex_file_full
Full path to the TeX template file (full result)
static struct StaticResource * key_error
Error: invalid gns key.
static MHD_RESULT create_response(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **ptr)
Send a response back to a connected client.
static struct StaticResource * index_full
Index file resource (full result).
static int continue_100
Used as a sort of singleton to send exactly one 100 CONTINUE per request.
static const struct ParameterMap pmap[]
Map of names with TeX definitions, used during PDF generation.
static struct MHD_Daemon * httpd
Handle to the HTTP server as provided by libmicrohttpd.
static char * tex_file_png
Full path to the TeX template file (PNG result)
static struct StaticResource * forbidden_error
Other errors.
static struct StaticResource * index_simple
Index file resource (simple result).
static void do_shutdown(void *cls)
Task ran at shutdown to clean up everything.
static void run_httpd(void)
Schedule a task to run MHD.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run.
static uint16_t port
Port number.
static void do_httpd(void *cls)
Called when the HTTP server has some pending operations.
static char * tex_file_simple
Full path to the TeX template file (simple result)
static struct GNUNET_SCHEDULER_Task * httpd_task
Our primary task for the HTTPD.
int main(int argc, char *const *argv)
The main function for gnunet-gns.
static struct StaticResource * internal_error
Errors after receiving the form data.
static struct StaticResource * open_static_resource(const char *name, const char *basedir)
Open a file on disk and generate a response for it.
static struct StaticResource * notfound_error
Error: 404.
static struct MHD_Response * response
Our canonical response.
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
static char * name
Name (label) of the records to list.
static char * res
Currently read line or NULL on EOF.
static char * value
Value of the record to add/remove.
#define MHD_RESULT
Data type to use for functions return an "MHD result".
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
char * GNUNET_DISK_mkdtemp(const char *t)
Create an (empty) temporary directory on disk.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_handle_size(struct GNUNET_DISK_FileHandle *fh, off_t *size)
Get the size of an open file.
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_NONE
Nobody is allowed to do anything to the file.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint16(char shortName, const char *name, const char *argumentHelp, const char *description, uint16_t *val)
Allow user to specify an uint16_t.
#define GNUNET_log(kind,...)
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_public_key_from_string(const char *str, struct GNUNET_CRYPTO_PublicKey *key)
Parses a (Base32) string representation of the public key.
@ GNUNET_SCHEDULER_PRIORITY_HIGH
Run with high priority (important requests).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
Releases the associated memory of an fd set.
void GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to, const fd_set *from, int nfds)
Copy a native fd set into the GNUnet representation.
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create(void)
Creates an fd set.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
char * GNUNET_OS_installation_get_path(const struct GNUNET_OS_ProjectData *pd, enum GNUNET_OS_InstallationPathKind dirkind)
Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX,...
@ GNUNET_OS_IPK_DATADIR
Return the directory where data is installed (share/gnunet/)
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio, struct GNUNET_TIME_Relative delay, const struct GNUNET_NETWORK_FDSet *rs, const struct GNUNET_NETWORK_FDSet *ws, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when any of the specified file descriptor set...
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
@ MHD_HTTP_BAD_REQUEST
Bad Request [RFC7231, Section 6.5.1].
@ MHD_HTTP_OK
OK [RFC7231, Section 6.3.1].
@ MHD_HTTP_NOT_FOUND
Not Found [RFC7231, Section 6.5.4].
@ MHD_HTTP_INTERNAL_SERVER_ERROR
Internal Server Error [RFC7231, Section 6.6.1].
@ MHD_HTTP_NOT_IMPLEMENTED
Not Implemented [RFC7231, Section 6.6.2].
static unsigned int size
Size of the "table".
An identity key as per LSD0001.
Handle used to access files (and pipes).
Definition of a command line option.
collection of IO descriptors
Entry in list of pending tasks.
Time for relative time used by GNUnet, in microseconds.
const char * name
Name of the parameter from the request.
const char * definition
Name of the definition in the TeX output.
struct MHD_Response * response
Cached response object to send to clients.
struct GNUNET_DISK_FileHandle * handle
Handle to file on disk.
uint64_t size
Size in bytes of the file.