1
0
mirror of https://github.com/fumiama/CMoe-Counter.git synced 2024-11-27 23:20:49 +01:00

优化代码结构, add c302

This commit is contained in:
源文雨 2022-11-23 14:39:37 +08:00
parent 57dfbfb4e7
commit 24ac4f3905
2 changed files with 143 additions and 110 deletions

View File

@ -22,6 +22,15 @@ RUN git clone --depth=1 https://github.com/fumiama/simple-http-server.git \
RUN rm -rf * RUN rm -rf *
RUN git clone --depth=1 https://github.com/fumiama/C302.git \
&& cd C302 \
&& mkdir build \
&& cd build \
&& cmake .. \
&& make install
RUN rm -rf *
COPY ./*.c . COPY ./*.c .
COPY ./*.h . COPY ./*.h .
COPY ./CMakeLists.txt . COPY ./CMakeLists.txt .
@ -35,9 +44,11 @@ FROM alpine:latest
COPY --from=builder /usr/local/bin/simple-http-server /usr/bin/simple-http-server COPY --from=builder /usr/local/bin/simple-http-server /usr/bin/simple-http-server
COPY --from=builder /usr/local/bin/cmoe /data/cmoe COPY --from=builder /usr/local/bin/cmoe /data/cmoe
COPY --from=builder /usr/local/bin/c302 /data/c302
COPY --from=builder /usr/local/lib/libspb.so /usr/local/lib/libspb.so COPY --from=builder /usr/local/lib/libspb.so /usr/local/lib/libspb.so
RUN chmod +x /usr/bin/simple-http-server RUN chmod +x /usr/bin/simple-http-server
RUN chmod +x /data/cmoe RUN chmod +x /data/cmoe
RUN chmod +x /data/c302
WORKDIR /data WORKDIR /data
COPY ./assets/favicon.ico . COPY ./assets/favicon.ico .

92
cmoe.c
View File

@ -35,7 +35,6 @@ static void http_error(ERRCODE code, char* msg) {
struct iovec iov[2] = {{&len, sizeof(uint32_t)}, {str, len}}; struct iovec iov[2] = {{&len, sizeof(uint32_t)}, {str, len}};
writev(1, &iov, 2); writev(1, &iov, 2);
free(str); free(str);
exit(EXIT_FAILURE);
} }
static char* get_arg(const char* query) { static char* get_arg(const char* query) {
@ -100,21 +99,28 @@ static uint32_t get_content_len(int isbig, uint16_t* len_type, char* cntstr) {
static void return_count(char* name, char* theme) { static void return_count(char* name, char* theme) {
FILE* fp = fopen(DATFILE, "rb+"); FILE* fp = fopen(DATFILE, "rb+");
if(!fp) fp = fopen(DATFILE, "wb+"); if(!fp) fp = fopen(DATFILE, "wb+");
if(fp) { if(!fp) {
http_error(HTTP500, "Open File Error.");
return;
}
flock(fileno(fp), LOCK_EX); flock(fileno(fp), LOCK_EX);
int ch, exist = 0, user_exist = 0; int ch, exist = 0, user_exist = 0;
char buf[sizeof(SIMPLE_PB)+sizeof(COUNTER)];
while(has_next(fp, ch)) { while(has_next(fp, ch)) {
SIMPLE_PB *spb = get_pb(fp); SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
COUNTER *d = (COUNTER *)spb->target; COUNTER *d = (COUNTER *)spb->target;
if (!strcmp(name, d->name)) { if (strcmp(name, d->name)) continue;
if(del_user(fp, spb)) http_error(HTTP500, "Unable to Delete Old Data."); if(del_user(fp, spb)) {
else { http_error(HTTP500, "Unable to Delete Old Data.");
if (add_user(d->name, d->count + 1, fp)) http_error(HTTP500, "Add User Error."); return;
else { }
if (add_user(d->name, d->count + 1, fp)) {
http_error(HTTP500, "Add User Error.");
return;
}
fclose(fp); fclose(fp);
char cntstrbuf[11]; char cntstrbuf[11];
sprintf(cntstrbuf, "%010u", d->count); sprintf(cntstrbuf, "%010u", d->count);
free(spb);
char* cntstr = cntstrbuf; char* cntstr = cntstrbuf;
for(int i = 0; i < 10; i++) if(cntstrbuf[i] != '0') { for(int i = 0; i < 10; i++) if(cntstrbuf[i] != '0') {
if(i > 2) cntstr = cntstrbuf+i-2; if(i > 2) cntstr = cntstrbuf+i-2;
@ -151,75 +157,91 @@ static void return_count(char* name, char* theme) {
printf(img_slot_rear); printf(img_slot_rear);
} }
printf(svg_tail); printf(svg_tail);
}
return; return;
} }
} else free(spb);
}
fclose(fp); fclose(fp);
http_error(HTTP404, "No Such User."); http_error(HTTP404, "No Such User.");
} else http_error(HTTP500, "Open File Error.");
} }
static int name_exist(char* name) { static int name_exist(char* name) {
FILE* fp = fopen(DATFILE, "rb+"); FILE* fp = fopen(DATFILE, "rb+");
if(!fp) fp = fopen(DATFILE, "wb+"); if(!fp) fp = fopen(DATFILE, "wb+");
if(fp) { if(fp) {
http_error(HTTP500, "Open File Error.");
exit(EXIT_FAILURE);
}
int ch, exist = 0; int ch, exist = 0;
char buf[sizeof(SIMPLE_PB)+sizeof(COUNTER)];
flock(fileno(fp), LOCK_EX); flock(fileno(fp), LOCK_EX);
while(has_next(fp, ch)) { while(has_next(fp, ch)) {
SIMPLE_PB *spb = get_pb(fp); SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
COUNTER *d = (COUNTER *)spb->target; COUNTER *d = (COUNTER *)spb->target;
if (!strcmp(name, d->name)) { if (!strcmp(name, d->name)) {
free(spb);
fclose(fp); fclose(fp);
return 1; return 1;
} }
else free(spb);
} }
fclose(fp); fclose(fp);
return 0; return 0;
} else http_error(HTTP500, "Open File Error.");
} }
#define QS (argv[2]) #define QS (argv[2])
// Usage: cmoe method query_string // Usage: cmoe method query_string
int main(int argc, char **argv) { int main(int argc, char **argv) {
if(argc == 3) { if(argc != 3) {
http_error(HTTP500, "Argument Count Error.");
return 1;
}
char* str = getenv("DATFILE"); char* str = getenv("DATFILE");
if(str != NULL) DATFILE = str; if(str != NULL) DATFILE = str;
str = getenv("TOKEN"); str = getenv("TOKEN");
if(str != NULL) TOKEN = str; if(str != NULL) TOKEN = str;
char* name = strstr(QS, "name="); char* name = strstr(QS, "name=");
items_len = align_struct(sizeof(COUNTER), 2, &counter.name, &counter.count); items_len = align_struct(sizeof(COUNTER), 2, &counter.name, &counter.count);
if(!items_len) http_error(HTTP500, "Align Struct Error."); if(!items_len) {
else if(name) { http_error(HTTP500, "Align Struct Error.");
return 2;
}
if(!name) {
http_error(HTTP400, "Name Argument Notfound.");
return 3;
}
name = get_arg(name + 5); name = get_arg(name + 5);
if(name) { if(!name) {
http_error(HTTP400, "Null Name Argument.");
return 4;
}
char* theme = strstr(QS, "theme="); char* theme = strstr(QS, "theme=");
if(theme) { if(theme) {
theme = get_arg(theme + 6); theme = get_arg(theme + 6);
} }
char* reg = strstr(QS, "reg="); char* reg = strstr(QS, "reg=");
if(reg) { if (!reg) {
return_count(name, theme);
return 0;
}
reg = get_arg(reg + 4); reg = get_arg(reg + 4);
if(reg) { if (!reg) {
if(strcmp(reg, TOKEN)) http_error(HTTP400, "Token Error."); http_error(HTTP400, "Null Register Token.");
else if(!name_exist(name)) { return 5;
}
if(strcmp(reg, TOKEN)) {
http_error(HTTP400, "Token Error.");
return 6;
}
if(name_exist(name)) {
http_error(HTTP400, "Name Exist.");
return 7;
}
FILE* fp = fopen(DATFILE, "ab+"); FILE* fp = fopen(DATFILE, "ab+");
if(fp) { if (!fp) {
http_error(HTTP500, "Open File Error.");
return 8;
}
flock(fileno(fp), LOCK_EX); flock(fileno(fp), LOCK_EX);
add_user(name, 0, fp); add_user(name, 0, fp);
fclose(fp); fclose(fp);
char* msg = "<P>Success.\r\n"; char* msg = "<P>Success.\r\n";
headers(strlen(msg), text/html); headers(strlen(msg), text/html);
write(1, msg, strlen(msg)); return write(1, msg, strlen(msg)) <= 0;
} else http_error(HTTP500, "Open File Error.");
} else http_error(HTTP400, "Name Exist.");
} else http_error(HTTP400, "Null Register Token.");
} else return_count(name, theme);
} else http_error(HTTP400, "Null Name Argument.");
} else http_error(HTTP400, "Name Argument Notfound.");
} else http_error(HTTP500, "Argument Count Error.");
return 0;
} }