#include <fcgi_stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SLN_ATTR_MAX 800000
#define SLN_MAX_VALUE_LEN 800000
#define SLN_MAX_FILENAME_LEN 800000
#define READ_POST_LEN 1000000
int write_file(char file_name[],char file_content[],long file_len);
int get_post_file_content(char**poststr,unsigned long long post_content_len);
int sln_cgi_input_multi_content_parse(const char *content_input,
unsigned long long content_length, const char *boundary);
int main()
{
char buff[100]={0};
int count = 0;
sprintf(buff, "------1-------malloc:%d----------------", count);
write_file("./log/malloc.log",buff,strlen(buff));
while(FCGI_Accept() >= 0)
{
//write_file("./log/lighttpd_test.log",buff,strlen(buff));
printf("Content-Type: multipart/form-data\r\n\r\n");
// printf("request id:%d,count:%d\r\n", getpid(),++count);
int i=0;
unsigned long long post_content_len=0;
char *req_method;
req_method = getenv("REQUEST_METHOD");
if (req_method == NULL)
{
printf("req_method == NULL\n");
return 1;
}
if (!strcmp(req_method, "POST"))
post_content_len=atoi(getenv("CONTENT_LENGTH"));
char* poststr=(char*)malloc(post_content_len);
if (poststr == NULL)
{
printf("malloc memory error\n");
return 1;
}
get_post_file_content(&poststr, post_content_len);
//printf("\r\n-----post_content_len:[%lld]]]]\n----------[[%s]]------------------------\r\n",
// post_content_len,poststr);
write_file("./log/lighttpd_test.log",poststr,post_content_len);
char boundary[100]={0};;
char *pboundary = strstr(poststr,"Content-Disposition:");
if (pboundary != NULL)
{
strncpy(boundary,poststr,pboundary-poststr-2);
sln_cgi_input_multi_content_parse(poststr,post_content_len, boundary);//现在只是demo,逻辑日后处理,再增加日志等内容
}
free(poststr);
memset(buff,0,sizeof(buff));
sprintf(buff, "{\n\"msg\": \"成功\",\n\"code\":%d\n}\n",++count);
fwrite(buff,1,strlen(buff),stdout);
}
memset(buff,0,sizeof(buff));
sprintf(buff, "------1-------free:%d----------------", count);
write_file("./log/free.log",buff,strlen(buff));
}
int sln_cgi_input_multi_content_parse(const char *content_input,
unsigned long long content_length, const char *boundary)
{
char attr[SLN_ATTR_MAX], value[SLN_MAX_VALUE_LEN],
filename[SLN_MAX_FILENAME_LEN], tfilename[SLN_MAX_FILENAME_LEN],
tmpname[SLN_MAX_FILENAME_LEN];
char *start, *end, *pname, *quote;
int boundary_len, attr_len, value_len, filename_len, file_size = 0;
FILE *fp = NULL;
char buff_file[200]={0};
if (boundary ==NULL)
printf("boundary ==NULL in sln_cgi_input_multi_content_parse\n");
boundary_len = strlen(boundary);
start = (char *)content_input;
if ((end = strstr(start, boundary))) {
start = end + boundary_len;
}
while (start)
{ // search boundary
attr[0] = '\0', filename[0] = '\0', filename[0] = '\0', tfilename[0] = '\0';
//printf("-------------while-----------\n");
if (0 == memcmp(start, "--\r\n", 4))
{ //reach the end
break;
}
else
{
start += 2;
}
end = strstr(start, "\r\n\r\n");
if (NULL == end)
{
break;
}
// get attr name
pname = strstr(start, "name=\"");
if (NULL == pname)
{
start = end + 1;
continue;
}
quote = strchr(pname + 6, '\"');
if (NULL == quote) {
start = end + 1;
continue;
}
attr_len = quote - (pname + 6);
strncpy(attr, pname + 6, attr_len);
attr[attr_len] = '\0';
//try to get filename
pname = strstr(start, "filename=\"");
if (NULL != pname) {
quote = strchr(pname + 10, '\"');
if (NULL == quote) {
start = end + 1;
continue;
}
filename_len = quote - (pname + 10);
strncpy(filename, pname + 10, filename_len);
filename[filename_len] = '\0';
// printf("filename:[%s]\n",filename);
}
start = end + 4;
if ('\0' == filename[0])
{
end = strstr(start, boundary);
// printf("0== filename[0])\n");
if (NULL != end) {
value_len = (end -1) - start; //"\r\n--"
if (value_len > sizeof(value)) {
value_len = sizeof(value) - 1;
}
strncpy(value, start, value_len);
value[value_len - 1] = '\0';
// printf("value:[%s]\n",value);
start = end + strlen(boundary);
}
}
else
{//处理文件
//改进解码,提高效率
char* end_file=NULL;
long int i=0;
char*p=start;
//printf("----------2---filename:[%s]\n",filename);
while (i<content_length - (start-content_input))
{
if(memcmp(p+i,boundary,strlen(boundary))==0)
break;
i++;
}
//continue;
i-=2;
if (i == 0)
{
printf("i=%d\n",i);
continue;
}
//strncpy(tfilename, start, i);
//printf("----------3---filename:[%s]\n",filename);
memset(buff_file,0,sizeof(buff_file));
sprintf(buff_file,"./server_file/%s",filename);
// printf("filename:[%s],filecontent_to_local:%s\n",filename,buff_file);
FILE*fw=fopen(buff_file,"wb");
fwrite(start, 1, i, fw);
fclose(fw);
start = p+strlen(boundary) + i;
}
}
return 0;
}
int get_post_file_content(char**poststr, unsigned long long post_content_len)
{
int sum = 0;
long read_len = 0;
while (sum < post_content_len)
{
read_len = fread(*poststr+sum, 1, READ_POST_LEN, stdin);
sum += read_len;
}
return 0;
}
int write_file(char file_name[],char file_content[],long file_len)
{
FILE*fw=fopen(file_name,"wb+");
fwrite(file_content, 1, file_len, fw);
fclose(fw);
return 0;
}