diff options
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | BUILDING | 3 | ||||
| -rw-r--r-- | LICENSE | 9 | ||||
| -rw-r--r-- | Makefile | 15 | ||||
| -rw-r--r-- | README | 9 | ||||
| -rw-r--r-- | cptc.c | 54 | ||||
| -rw-r--r-- | cptc.h | 16 | ||||
| -rw-r--r-- | main.c | 10 | ||||
| -rw-r--r-- | requestHandler.c | 70 |
9 files changed, 189 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..405c16a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +compile_flags.txt +*.o +cptc diff --git a/BUILDING b/BUILDING new file mode 100644 index 0000000..d5df84f --- /dev/null +++ b/BUILDING @@ -0,0 +1,3 @@ +This program depends on the cpetpet* library that depends on the imagemagick. + +* https://github.com/nakidai/cpetpet diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..058f14e --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +Copyright 2024 nakidai + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d2ea389 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +OBJS += cptc.o +OBJS += main.o +OBJS += requestHandler.o + +CFLAGS += -std=c11 +RM = rm -f + +all: cptc + +cptc: $(OBJS) + +clean: + $(RM) cptc *.o + +cptc: $(OBJS) diff --git a/README b/README new file mode 100644 index 0000000..0c46741 --- /dev/null +++ b/README @@ -0,0 +1,9 @@ +CPTC (https://github.com/nakidai/cptc) +-------------------------------------- +PetTheCord but rewritten in the C cuz some people was annoying me about that a +lot. + +Paths: +/ Show this text +/<UID>.* Return a gif that pets given UID + - It will return 403 with some explanation in entity on error. diff --git a/cptc.c b/cptc.c new file mode 100644 index 0000000..60c1954 --- /dev/null +++ b/cptc.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <unistd.h> + +#include "cptc.h" + + +#define error(text, status) \ + do \ + { \ + perror(text); \ + exit(status); \ + } while (0) \ + + +void CPTC(const char *ip, in_port_t port) +{ + struct sockaddr_in addr, peer; + socklen_t peer_size; + int fd, peerfd; + + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + error("socket()", 1); + + addr = (struct sockaddr_in) + { + .sin_addr.s_addr = inet_addr(ip), + .sin_port = htons(port), + .sin_family = AF_INET, + }; + + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) + error("bind()", 1); + if (listen(fd, 128) == -1) + error("listen()", 3); + + printf("Started serving at %s:%d\n", ip, port); + for (;;) + { + peer_size = sizeof(peer); + if ((peerfd = accept(fd, (struct sockaddr *)&peer, &peer_size)) < 0) + { + perror("accept()"); + continue; + } + printf("Connection from %s:%u!\n", inet_ntoa(peer.sin_addr), peer.sin_port); + + CPTC_requestHandler(peerfd); + close(peerfd); + } +} diff --git a/cptc.h b/cptc.h new file mode 100644 index 0000000..aa14648 --- /dev/null +++ b/cptc.h @@ -0,0 +1,16 @@ +#ifndef __CPTC_H__ +#define __CPTC_H__ + +#include <netinet/in.h> + + +enum CPTC_Method +{ + CPTC_GET = 'G', + CPTC_HEAD = 'H', +}; + +void CPTC(const char *address, in_port_t port); +void CPTC_requestHandler(int fd); + +#endif /* __CPTC_H__ */ diff --git a/main.c b/main.c new file mode 100644 index 0000000..7a63ebe --- /dev/null +++ b/main.c @@ -0,0 +1,10 @@ +#include <fcntl.h> +#include <unistd.h> + +#include "cptc.h" + + +int main(void) +{ + CPTC("127.0.0.1", 8080); +} diff --git a/requestHandler.c b/requestHandler.c new file mode 100644 index 0000000..da022dd --- /dev/null +++ b/requestHandler.c @@ -0,0 +1,70 @@ +#include "cptc.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> + + + +static const char *ok = "HTTP/1.0 200 OK\r\n"; +static const char *not_implemented = "HTTP/1.0 501 Not implemented\r\n"; + +static const char *content_length = "Content-Length: %d\r\n"; +static const char *text_plain = "Content-Type: text/plain\r\n"; + +static const char *end = "\r\n"; + +static const char *root = "\ +CPTC (https://github.com/nakidai/cptc)\n\ +--------------------------------------\n\ +PetTheCord but rewritten in the C cuz some people was annoying me about that a\n\ +lot.\n\ +\n\ +Paths:\n\ +/ Show this text\n\ +/<UID>.* Return a gif that pets given UID\n"; + +void CPTC_requestHandler(int fd) +{ + enum CPTC_Method method; + char *path; + char *buffer = (char *)malloc(sizeof(*buffer) * 512); + ssize_t received = recv(fd, buffer, sizeof(*buffer) * 512, 0); + + if (received == -1) + { + perror("recv()"); + goto end; + } + + printf("Got request!\n%s\n", buffer); + + method = buffer[0]; + if (method != CPTC_GET && method != CPTC_HEAD) + { + send(fd, not_implemented, strlen(not_implemented), 0); + goto end; + } + + path = strchr(buffer, ' ') + 1; + for (int i = 0; i < strchr(path, ' ') - path; ++i) + putchar(*(path + i)); + putchar('\n'); + if (strchr(path, ' ') - path == 1) + { + send(fd, ok, strlen(ok), 0); + char *length = malloc(sizeof(*length) * 32); + snprintf(length, 32, content_length, strlen(root)); + send(fd, text_plain, strlen(text_plain), 0); + send(fd, length, strlen(length), 0); + send(fd, end, strlen(end), 0); + send(fd, root, strlen(root), 0); + free(length); + goto end; + } + +end: + free(buffer); +} |