1
0
mirror of https://github.com/fumiama/CMoe-Counter.git synced 2024-11-24 06:10:11 +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 .

240
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,126 +99,149 @@ 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) {
flock(fileno(fp), LOCK_EX); http_error(HTTP500, "Open File Error.");
int ch, exist = 0, user_exist = 0; return;
while(has_next(fp, ch)) { }
SIMPLE_PB *spb = get_pb(fp); flock(fileno(fp), LOCK_EX);
COUNTER *d = (COUNTER *)spb->target; int ch, exist = 0, user_exist = 0;
if (!strcmp(name, d->name)) { char buf[sizeof(SIMPLE_PB)+sizeof(COUNTER)];
if(del_user(fp, spb)) http_error(HTTP500, "Unable to Delete Old Data."); while(has_next(fp, ch)) {
else { SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
if (add_user(d->name, d->count + 1, fp)) http_error(HTTP500, "Add User Error."); COUNTER *d = (COUNTER *)spb->target;
else { if (strcmp(name, d->name)) continue;
fclose(fp); if(del_user(fp, spb)) {
char cntstrbuf[11]; http_error(HTTP500, "Unable to Delete Old Data.");
sprintf(cntstrbuf, "%010u", d->count); return;
free(spb); }
char* cntstr = cntstrbuf; if (add_user(d->name, d->count + 1, fp)) {
for(int i = 0; i < 10; i++) if(cntstrbuf[i] != '0') { http_error(HTTP500, "Add User Error.");
if(i > 2) cntstr = cntstrbuf+i-2; return;
break;
}
int isbig = 0;
char** theme_type = mb;
uint16_t* len_type = mbl;
if(theme) {
set_type("mbh", mbh, mbhl) else
set_type("r34", r34, r34l) else
set_type("gb", gb, gbl) else
set_type("gbh", gbh, gbhl)
isbig = (theme_type == gb || theme_type == gbh);
}
int w, h;
char *head;
if(isbig) {
w = W_BIG;
h = H_BIG;
head = svg_big;
}
else {
w = W_SMALL;
h = H_SMALL;
head = svg_small;
}
headers(get_content_len(isbig, len_type, cntstr), image/svg+xml);
printf(head, w*(10+cntstrbuf-cntstr));
for(int i = 0; cntstr[i]; i++) {
printf(img_slot_front, w * i, w, h);
int n = cntstr[i] - '0';
fwrite(theme_type[n], len_type[n], 1, stdout);
printf(img_slot_rear);
}
printf(svg_tail);
}
return;
}
} else free(spb);
} }
fclose(fp); fclose(fp);
http_error(HTTP404, "No Such User."); char cntstrbuf[11];
} else http_error(HTTP500, "Open File Error."); sprintf(cntstrbuf, "%010u", d->count);
char* cntstr = cntstrbuf;
for(int i = 0; i < 10; i++) if(cntstrbuf[i] != '0') {
if(i > 2) cntstr = cntstrbuf+i-2;
break;
}
int isbig = 0;
char** theme_type = mb;
uint16_t* len_type = mbl;
if(theme) {
set_type("mbh", mbh, mbhl) else
set_type("r34", r34, r34l) else
set_type("gb", gb, gbl) else
set_type("gbh", gbh, gbhl)
isbig = (theme_type == gb || theme_type == gbh);
}
int w, h;
char *head;
if(isbig) {
w = W_BIG;
h = H_BIG;
head = svg_big;
}
else {
w = W_SMALL;
h = H_SMALL;
head = svg_small;
}
headers(get_content_len(isbig, len_type, cntstr), image/svg+xml);
printf(head, w*(10+cntstrbuf-cntstr));
for(int i = 0; cntstr[i]; i++) {
printf(img_slot_front, w * i, w, h);
int n = cntstr[i] - '0';
fwrite(theme_type[n], len_type[n], 1, stdout);
printf(img_slot_rear);
}
printf(svg_tail);
return;
}
fclose(fp);
http_error(HTTP404, "No Such User.");
} }
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) {
int ch, exist = 0; http_error(HTTP500, "Open File Error.");
flock(fileno(fp), LOCK_EX); exit(EXIT_FAILURE);
while(has_next(fp, ch)) { }
SIMPLE_PB *spb = get_pb(fp); int ch, exist = 0;
COUNTER *d = (COUNTER *)spb->target; char buf[sizeof(SIMPLE_PB)+sizeof(COUNTER)];
if (!strcmp(name, d->name)) { flock(fileno(fp), LOCK_EX);
free(spb); while(has_next(fp, ch)) {
fclose(fp); SIMPLE_PB *spb = read_pb_into(fp, (SIMPLE_PB*)buf);
return 1; COUNTER *d = (COUNTER *)spb->target;
} if (!strcmp(name, d->name)) {
else free(spb); fclose(fp);
return 1;
} }
fclose(fp); }
return 0; fclose(fp);
} else http_error(HTTP500, "Open File Error."); return 0;
} }
#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) {
char* str = getenv("DATFILE"); http_error(HTTP500, "Argument Count Error.");
if(str != NULL) DATFILE = str; return 1;
str = getenv("TOKEN"); }
if(str != NULL) TOKEN = str; char* str = getenv("DATFILE");
char* name = strstr(QS, "name="); if(str != NULL) DATFILE = str;
items_len = align_struct(sizeof(COUNTER), 2, &counter.name, &counter.count); str = getenv("TOKEN");
if(!items_len) http_error(HTTP500, "Align Struct Error."); if(str != NULL) TOKEN = str;
else if(name) { char* name = strstr(QS, "name=");
name = get_arg(name + 5); items_len = align_struct(sizeof(COUNTER), 2, &counter.name, &counter.count);
if(name) { if(!items_len) {
char* theme = strstr(QS, "theme="); http_error(HTTP500, "Align Struct Error.");
if(theme) { return 2;
theme = get_arg(theme + 6); }
} if(!name) {
char* reg = strstr(QS, "reg="); http_error(HTTP400, "Name Argument Notfound.");
if(reg) { return 3;
reg = get_arg(reg + 4); }
if(reg) { name = get_arg(name + 5);
if(strcmp(reg, TOKEN)) http_error(HTTP400, "Token Error."); if(!name) {
else if(!name_exist(name)) { http_error(HTTP400, "Null Name Argument.");
FILE* fp = fopen(DATFILE, "ab+"); return 4;
if(fp) { }
flock(fileno(fp), LOCK_EX); char* theme = strstr(QS, "theme=");
add_user(name, 0, fp); if(theme) {
fclose(fp); theme = get_arg(theme + 6);
char* msg = "<P>Success.\r\n"; }
headers(strlen(msg), text/html); char* reg = strstr(QS, "reg=");
write(1, msg, strlen(msg)); if (!reg) {
} else http_error(HTTP500, "Open File Error."); return_count(name, theme);
} else http_error(HTTP400, "Name Exist."); return 0;
} else http_error(HTTP400, "Null Register Token."); }
} else return_count(name, theme); reg = get_arg(reg + 4);
} else http_error(HTTP400, "Null Name Argument."); if (!reg) {
} else http_error(HTTP400, "Name Argument Notfound."); http_error(HTTP400, "Null Register Token.");
} else http_error(HTTP500, "Argument Count Error."); return 5;
return 0; }
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+");
if (!fp) {
http_error(HTTP500, "Open File Error.");
return 8;
}
flock(fileno(fp), LOCK_EX);
add_user(name, 0, fp);
fclose(fp);
char* msg = "<P>Success.\r\n";
headers(strlen(msg), text/html);
return write(1, msg, strlen(msg)) <= 0;
} }