aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2020-12-24 16:48:39 +0100
committerOmar Polo <op@omarpolo.com>2020-12-24 16:48:39 +0100
commit488f059ac4c75b8b2e0e610e6445640bce8152e8 (patch)
treeb9c00f8fdfb6fcf5fc90e1b095b278ed72765d53
parent83000e2dd417e81dabfcad740af63d1ee86e1389 (diff)
moving declarations to header file
-rw-r--r--gmid.c149
-rw-r--r--gmid.h154
2 files changed, 155 insertions, 148 deletions
diff --git a/gmid.c b/gmid.c
index b20047e..7376a34 100644
--- a/gmid.c
+++ b/gmid.c
@@ -18,131 +18,15 @@
#include <sys/socket.h>
#include <sys/stat.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
-#include <poll.h>
#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
-#include <syslog.h>
-#include <tls.h>
-#include <unistd.h>
-
-#ifndef __OpenBSD__
-# define pledge(a, b) 0
-# define unveil(a, b) 0
-#endif
-
-#ifndef INFTIM
-# define INFTIM -1
-#endif
-
-#define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
-/* large enough to hold a copy of a gemini URL and still have extra room */
-#define PATHBUF 2048
-
-#define SUCCESS 20
-#define TEMP_FAILURE 40
-#define NOT_FOUND 51
-#define BAD_REQUEST 59
-
-#ifndef MAX_USERS
-#define MAX_USERS 64
-#endif
-
-#define SAFE_SETENV(var, val) do { \
- const char *_tmp = (val); \
- if (_tmp == NULL) \
- _tmp = ""; \
- setenv((var), _tmp, 1); \
- } while(0)
-
-#define LOG(priority, c, fmt, ...) \
- do { \
- char buf[INET_ADDRSTRLEN]; \
- if (inet_ntop((c)->af, &(c)->addr, \
- buf, sizeof(buf)) == NULL) \
- FATAL("inet_ntop: %s", strerror(errno)); \
- if (foreground) \
- fprintf(stderr, \
- "%s " fmt "\n", buf, __VA_ARGS__); \
- else \
- syslog((priority) | LOG_DAEMON, \
- "%s " fmt, buf, __VA_ARGS__); \
- } while (0)
-
-#define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__)
-#define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__)
-#define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__)
-#define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__)
-
-#define FATAL(fmt, ...) \
- do { \
- if (foreground) \
- fprintf(stderr, fmt "\n", __VA_ARGS__); \
- else \
- syslog(LOG_DAEMON | LOG_CRIT, \
- fmt, __VA_ARGS__); \
- exit(1); \
- } while (0)
-
-enum {
- S_OPEN,
- S_INITIALIZING,
- S_SENDING,
- S_CLOSING,
-};
-
-struct client {
- struct tls *ctx;
- int state;
- int code;
- const char *meta;
- int fd, waiting_on_child;
- pid_t child;
- char sbuf[1024]; /* static buffer */
- void *buf, *i; /* mmap buffer */
- ssize_t len, off; /* mmap/static buffer */
- int af;
- struct in_addr addr;
-};
-
-enum {
- FILE_EXISTS,
- FILE_EXECUTABLE,
- FILE_DIRECTORY,
- FILE_MISSING,
-};
-
-struct etm { /* file extension to mime */
- const char *mime;
- const char *ext;
-} filetypes[] = {
- {"application/pdf", "pdf"},
-
- {"image/gif", "gif"},
- {"image/jpeg", "jpg"},
- {"image/jpeg", "jpeg"},
- {"image/png", "png"},
- {"image/svg+xml", "svg"},
-
- {"text/gemini", "gemini"},
- {"text/gemini", "gmi"},
- {"text/markdown", "markdown"},
- {"text/markdown", "md"},
- {"text/plain", "txt"},
- {"text/xml", "xml"},
-
- {NULL, NULL}
-};
+#include "gmid.h"
const char *dir, *cgi;
int dirfd;
@@ -150,37 +34,6 @@ int port;
int foreground;
int connected_clients;
-void siginfo_handler(int);
-int starts_with(const char*, const char*);
-
-char *url_after_proto(char*);
-char *url_start_of_request(char*);
-int url_trim(struct client*, char*);
-char *adjust_path(char*);
-ssize_t filesize(int);
-
-int start_reply(struct pollfd*, struct client*, int, const char*);
-const char *path_ext(const char*);
-const char *mime(const char*);
-int check_path(struct client*, const char*, int*);
-int check_for_cgi(char *, char*, struct pollfd*, struct client*);
-int open_file(char*, char*, struct pollfd*, struct client*);
-int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
-void cgi_setpoll_on_child(struct pollfd*, struct client*);
-void cgi_setpoll_on_client(struct pollfd*, struct client*);
-void handle_cgi(struct pollfd*, struct client*);
-void send_file(char*, char*, struct pollfd*, struct client*);
-void send_dir(char*, struct pollfd*, struct client*);
-void handle(struct pollfd*, struct client*);
-
-void mark_nonblock(int);
-int make_soket(int);
-void do_accept(int, struct tls*, struct pollfd*, struct client*);
-void goodbye(struct pollfd*, struct client*);
-void loop(struct tls*, int);
-
-void usage(const char*);
-
void
siginfo_handler(int sig)
{
diff --git a/gmid.h b/gmid.h
new file mode 100644
index 0000000..e4d53db
--- /dev/null
+++ b/gmid.h
@@ -0,0 +1,154 @@
+#ifndef GMID_H
+#define GMID_H
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
+#include <poll.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <syslog.h>
+#include <tls.h>
+#include <unistd.h>
+
+#ifndef __OpenBSD__
+# define pledge(a, b) 0
+# define unveil(a, b) 0
+#endif
+
+#ifndef INFTIM
+# define INFTIM -1
+#endif
+
+#define GEMINI_URL_LEN (1024+3) /* URL max len + \r\n + \0 */
+
+/* large enough to hold a copy of a gemini URL and still have extra room */
+#define PATHBUF 2048
+
+#define SUCCESS 20
+#define TEMP_FAILURE 40
+#define NOT_FOUND 51
+#define BAD_REQUEST 59
+
+#ifndef MAX_USERS
+#define MAX_USERS 64
+#endif
+
+#define SAFE_SETENV(var, val) do { \
+ const char *_tmp = (val); \
+ if (_tmp == NULL) \
+ _tmp = ""; \
+ setenv((var), _tmp, 1); \
+ } while(0)
+
+#define LOG(priority, c, fmt, ...) \
+ do { \
+ char buf[INET_ADDRSTRLEN]; \
+ if (inet_ntop((c)->af, &(c)->addr, \
+ buf, sizeof(buf)) == NULL) \
+ FATAL("inet_ntop: %s", strerror(errno)); \
+ if (foreground) \
+ fprintf(stderr, \
+ "%s " fmt "\n", buf, __VA_ARGS__); \
+ else \
+ syslog((priority) | LOG_DAEMON, \
+ "%s " fmt, buf, __VA_ARGS__); \
+ } while (0)
+
+#define LOGE(c, fmt, ...) LOG(LOG_ERR, c, fmt, __VA_ARGS__)
+#define LOGN(c, fmt, ...) LOG(LOG_NOTICE, c, fmt, __VA_ARGS__)
+#define LOGI(c, fmt, ...) LOG(LOG_INFO, c, fmt, __VA_ARGS__)
+#define LOGD(c, fmt, ...) LOG(LOG_DEBUG, c, fmt, __VA_ARGS__)
+
+#define FATAL(fmt, ...) \
+ do { \
+ if (foreground) \
+ fprintf(stderr, fmt "\n", __VA_ARGS__); \
+ else \
+ syslog(LOG_DAEMON | LOG_CRIT, \
+ fmt, __VA_ARGS__); \
+ exit(1); \
+ } while (0)
+
+enum {
+ S_OPEN,
+ S_INITIALIZING,
+ S_SENDING,
+ S_CLOSING,
+};
+
+struct client {
+ struct tls *ctx;
+ int state;
+ int code;
+ const char *meta;
+ int fd, waiting_on_child;
+ pid_t child;
+ char sbuf[1024]; /* static buffer */
+ void *buf, *i; /* mmap buffer */
+ ssize_t len, off; /* mmap/static buffer */
+ int af;
+ struct in_addr addr;
+};
+
+enum {
+ FILE_EXISTS,
+ FILE_EXECUTABLE,
+ FILE_DIRECTORY,
+ FILE_MISSING,
+};
+
+struct etm { /* file extension to mime */
+ const char *mime;
+ const char *ext;
+} filetypes[] = {
+ {"application/pdf", "pdf"},
+
+ {"image/gif", "gif"},
+ {"image/jpeg", "jpg"},
+ {"image/jpeg", "jpeg"},
+ {"image/png", "png"},
+ {"image/svg+xml", "svg"},
+
+ {"text/gemini", "gemini"},
+ {"text/gemini", "gmi"},
+ {"text/markdown", "markdown"},
+ {"text/markdown", "md"},
+ {"text/plain", "txt"},
+ {"text/xml", "xml"},
+
+ {NULL, NULL}
+};
+
+void siginfo_handler(int);
+int starts_with(const char*, const char*);
+
+char *url_after_proto(char*);
+char *url_start_of_request(char*);
+int url_trim(struct client*, char*);
+char *adjust_path(char*);
+ssize_t filesize(int);
+
+int start_reply(struct pollfd*, struct client*, int, const char*);
+const char *path_ext(const char*);
+const char *mime(const char*);
+int check_path(struct client*, const char*, int*);
+int check_for_cgi(char *, char*, struct pollfd*, struct client*);
+int open_file(char*, char*, struct pollfd*, struct client*);
+int start_cgi(const char*, const char*, const char*, struct pollfd*, struct client*);
+void cgi_setpoll_on_child(struct pollfd*, struct client*);
+void cgi_setpoll_on_client(struct pollfd*, struct client*);
+void handle_cgi(struct pollfd*, struct client*);
+void send_file(char*, char*, struct pollfd*, struct client*);
+void send_dir(char*, struct pollfd*, struct client*);
+void handle(struct pollfd*, struct client*);
+
+void mark_nonblock(int);
+int make_soket(int);
+void do_accept(int, struct tls*, struct pollfd*, struct client*);
+void goodbye(struct pollfd*, struct client*);
+void loop(struct tls*, int);
+
+void usage(const char*);
+
+#endif