📄 web_task.c
字号:
//-------------------------------------------------------
// 颇老疙: web_task.c
//-------------------------------------------------------
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include "web_server.h"
#include "web_task.h"
//-------------------------------------------------------
// extractRequest 窃荐, 夸没蜡屈, URL阑 st_req 磊丰备炼俊 历厘
//-------------------------------------------------------
int extractRequest(st_req* request) {
int loop = 0, pos, flag = 0;
char* str;
str = (char*)request->msg;
if(strncmp(str, "GET ", strlen("GET ")) == 0) {// 夸没蜡屈 技泼
loop = loop + strlen("GET ");
request->method = METHOD_GET;
}
else if(strncmp(str, "POST ", strlen("POST ")) == 0) {
loop = loop + strlen("POST ");
request->method = METHOD_POST;
}
else return ANU_FALSE; //夸没蜡屈捞 GET, POST啊 酒聪搁 -1府畔
for(pos=0; loop<strlen(str); loop++) {// URL Setting
if(str[loop]=='\r'||str[loop]=='\n'||str[loop]==' '||str[loop]=='?'){
request->url[pos] = '\0';
break;
}
else {
if(flag) request->url[pos++] = str[loop];
else flag = 1;
}
}
if(strstr(request->url, ".gif") != NULL) // 捞固瘤 颇老狼 夸没 咯何 眉农
request->is_img = ANU_TRUE;
else
request->is_img = ANU_FALSE;
return ANU_TRUE;
}
//-------------------------------------------------------
// checkAuthorize 窃荐
//-------------------------------------------------------
void checkAuthorize(st_req* request){
int loop, len;
char temp[100], passwd[100];
char *str;
str = request->msg;
len = strlen(str);
for(loop=0; loop<len; loop++) {
if(strncmp(str, "Authorization: Basic ", strlen("Authorization: Basic "))==0){
str += strlen("Authorization: Basic ");
strncpy(temp, str, 99);
base64_decode(temp); //菩胶况靛 base64 叼内爹
strcpy(passwd, "guest:");
strcat(passwd, "guest");
if(strcmp(temp, passwd) == 0) { //菩胶况靛 犬牢
request->is_logged = ANU_TRUE;
return;
}
}
else
str++;
}
request->is_logged = ANU_FALSE;
}
//-------------------------------------------------------
// base64_decode 窃荐
//-------------------------------------------------------
void base64_decode(char* s)
{
char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int bit_offset, byte_offset, idx, i, n;
unsigned char *d = (unsigned char*)s;
char *p;
n=i=0;
while (*s && (p=strchr(b64,*s))) {
idx = (int)(p - b64);
byte_offset = (i*6)/8;
bit_offset = (i*6)%8;
d[byte_offset] &= ~((1<<(8-bit_offset))-1);
if (bit_offset < 3) {
d[byte_offset] |= (idx << (2-bit_offset));
n = byte_offset+1;
} else {
d[byte_offset] |= (idx >> (bit_offset-2));
d[byte_offset+1] = 0;
d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
n = byte_offset+2;
}
s++; i++;
}
d[n] = 0;
}
//-------------------------------------------------------
// setRequestPostValue 窃荐
//-------------------------------------------------------
int setRequestPostValue(st_req* request){
int pair_num = 0, loop, pos, post_length;
char pair_value[POST_NAME_MAX+POST_VAL_MAX]; //茄街狼 post value '&'啊 唱坷扁 傈鳖瘤狼 蔼
char post_temp[HTTP_POST_MAX]={'\0',};
char *msg = (char*)request->msg;
/* POST 皋矫瘤狼 夯巩 茫扁 */
for(loop=0; loop<strlen(msg)-3; loop++) {
if(msg[loop]=='\r'&&msg[loop+1]=='\n'&&msg[loop+2]=='\r'&&msg[loop+3]=='\n'){
msg = msg+loop+4;
strcpy(post_temp, msg);
break;
}
}
/* POST 皋矫瘤狼 疙飞绢 牢磊 茫扁 */
pos=0;
post_length = strlen(post_temp)+1; /* 付瘤阜 函荐蔼阑 历厘窍扁 困秦 */
for(loop=0; loop<post_length; loop++) {
if(post_temp[loop] == '&' || post_temp[loop] == '\0' || post_temp[loop] == ' ') {
pair_value[pos] = '\0';
makePairPostValue(pair_value, pair_num, request);
pair_num++;
pos=0;
if(post_temp[loop] == '\0') break;
else continue;
}
pair_value[pos] = post_temp[loop];
pos++;
}
return pair_num;
}
//-------------------------------------------------------
// makePairPostValue 窃荐
//-------------------------------------------------------
void makePairPostValue(char* together, int pair_cnt, st_req *request)
{
int loop, pos;
/* name setting */
for(loop=0; together[loop] != '=' && together[loop] != '\0'; loop++)
request->post_values[pair_cnt].name[loop] = together[loop];
request->post_values[pair_cnt].name[loop] = '\0';
loop++; /* remove '=' character */
/* value setting*/
for(pos=0; together[loop] != '\0'; loop++) {
if(loop > strlen(together))
break;
request->post_values[pair_cnt].value[pos] = together[loop];
pos++;
}
request->post_values[pair_cnt].value[pos] = '\0';
}
//-------------------------------------------------------
// readAndSend 窃荐
//-------------------------------------------------------
void readAndSend(int client_sd, char *html)
{
FILE *f;
char temp[500];
char url[100];
int i;
strcpy(url, HtmlDir);
strcat(url, html);
f = fopen(url, "r");
while((i = fread(temp, 1, 500, f)) != 0) {
write(client_sd, temp, i);
}
fclose(f);
}
//-------------------------------------------------------
// execute 窃荐
//-------------------------------------------------------
int execute(st_req* request, int pair_num) {
char command[100], redirection[] = {" > ./html/execute.html"};
int i, com_length;
for(i = 0; i < pair_num; i++) {
if(strcmp((request->post_values[i]).name, "exec") == 0)
strcpy(command, (request->post_values[i]).value);
else
break;
}
strcat(command, redirection);
com_length = strlen(command);
for( i = 0; i < com_length; i++) {
if ( command[i] == '\n'||command[i] == '\r'||command[i] == '+'){
command[i] = ' ';
}
}
printf("%s\n", command);
system(command);
readAndSend(request->client_sd, "form1.html");
readAndSend(request->client_sd, "execute.html");
readAndSend(request->client_sd, "form2.html");
return 0;
}
//------------------- end of file -------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -