Пример #1
0
static void		launch_dir_lst(t_dlst *headdir, t_info *info)
{
	t_dlst		*it;
	t_node		*tmp;

	if (!dlst_empty(headdir))
	{
		sort_merge_lst(headdir, info);
		it = headdir->next;
		tmp = C_NODE(t_node, it);
		clear_head(&info->headfile);
		while (it != headdir)
		{
			tmp = C_NODE(t_node, it);
			if (!dlst_is_singular(headdir) || info->flush)
			{
				if (it != headdir->next)
					ft_printf("\n%s:\n", tmp->namtyp.d_name);
				else
					ft_printf("%s:\n", tmp->namtyp.d_name);
			}
			ft_get_dir(tmp->path, info);
			it = it->next;
		}
	}
	clear_head(headdir);
}
Пример #2
0
static void * accept_request(void* arg)
{
	int sock = (int)arg;
	printf("%d\n",sock);
	char buf[SIZE];
	int ret = 0;
#ifdef _DEBUG_
	do
	{
			ret = get_line(sock,buf,size);
			printf("%s",buf);
			fflush(stdout);
	}while(ret > 0 && strcmp(buf,'\n'));
#endif
	char method[SIZE / 10];
	char url[SIZE];

	char* query_string = NULL;
	int cgi = 0;
	char path[SIZE];
	memset(method,'\0',sizeof(method));
	memset(url,'\0',sizeof(url));

	ret = get_line(sock,buf,sizeof(buf));
	if(ret < 0)
	{
			echo_errno(sock,1);
			return (void*)1;
	}
	printf("%s\n",buf);
	//1 get method
	//GET //xx//yy HTTP/1.1
	int i = 0;
	int j =0;
	while(i< sizeof(method) - 1 && !isspace(buf[j])  && j < sizeof(buf) )
	{
			method[i]=buf[j];
			++i;
			++j;
	}

	method[i] = '\0';

	//2 check method
	if(strcasecmp(method,"GET")!=0 && strcasecmp(method,"POST") != 0)
	{
			echo_errno(sock,1);
			return (void*)2;
	}

	//3 get url first step space
	while(isspace(buf[j]))
	{
			++j;
	}

	i=0;
	while(!isspace(buf[j])&& (i < sizeof(url)-1) && j < sizeof(buf))
	{
		url[i] = buf [j];
		++i;
		++j;

	}

	if(strcasecmp(method,"POST") == 0)
	{
			cgi = 1;
	}
	printf("method: %s,url_path :%s\n ",method,url);

	if(strcasecmp(method,"GET") == 0)
	{
			query_string = url;
			
	}

	char *start = url;
	while( *start != '\0')
	{
			if( *start == '?')
			{
					cgi = 1;
					*start = '\0';
					query_string = start+1;
					break;
			}

			++start;

	}
	sprintf(path,"htdoc%s",url);
	if(path[strlen(path)-1] == '/')
	{
			strcat(path,"index.html");
	}

	//
	printf("path:%s\n",path);
	printf("cgi:%d\n",cgi);
	//method,query_string,cgi,path
	struct stat st;
	if(stat(path,&st) < 0)//default -> htdoc/index.html
	{
			printf("stat error\n");
			echo_errno(sock,1);
			return (void*)-3;
			
	}
	else
	{
			if(S_ISDIR(st.st_mode))
			{
					strcpy(path,"htdoc/index.html");
			}
			if(( st.st_mode & S_IXGRP)||\
					( st.st_mode & S_IXGRP)||\
					st.st_mode & S_IXOTH)
			{
					cgi = 1;
			}
			else
			{}

			//path cgi
			if(cgi)
			{
					printf("cgi mode\n");
					execut_cgi(sock,path,method,query_string);
			}
			else// 请求首页内容
			{
					clear_head(sock);
					echo_www(sock,path,st.st_size);
			}
	}

	close(sock);   //no face link
	return (void*) 0;
}
Пример #3
0
static void execut_cgi(int sock,const char* path,const char* method,const char* query_string)
{
		int content_len = -1;
		char buf[SIZE];
		memset(buf,'\0',sizeof(buf));
		int cgi_input[2];
		int cgi_output[2];
		
		printf("method: %s\n",method);


		if(strcasecmp(method,"GET") == 0)
		{
				clear_head(sock);
		}else  //POST
		{
				printf("aaaaaaaaaaaaaaaa\n");
				int ret = 0;
				do{
						ret = get_line(sock,buf,sizeof(buf));
						if(strncasecmp(buf,"Content-Length: ",16) == 0)
						{
								content_len = atoi(&buf[16]);				
						}
				}while((ret > 0) && strcmp(buf,"\n") != 0);
				printf("contlen:%d\n",content_len);
				if(content_len == -1)
				{
						echo_errno(sock,1);
						return ;
				}
		}
		if(pipe(cgi_output) < 0)
		{
			echo_errno(sock,1);
			return ;
		}
		if(pipe(cgi_input) < 0)
		{
			echo_errno(sock,1);
			return ;
		}

				
		sprintf(buf,"HTTP/1.0 200 OK\r\n\r\n");
		send(sock,buf,strlen(buf),0);
		pid_t id = fork();
		// 处理客户端请求
		if(id == 0)//child
		{
				close(cgi_input[1]);
				close(cgi_output[0]);
		
				dup2(cgi_input[0],0);
				dup2(cgi_output[1],1);
				
				sprintf(buf,"REQUEST_METHOD=%s",method);
				putenv(buf);

				if(strcasecmp(method,"GET") == 0)
				{
						sprintf(buf,"QUERY_STRING=%s",query_string);
						putenv(buf);
				}else
				{
						sprintf(buf,"CONTENT_LENGTH=%d",content_len);
						putenv(buf);
				}
				execl(path,path,NULL);
				exit(1);
		}else  //father
		{
				close(cgi_input[0]);
				close(cgi_output[1]);

				int i=0;
				char c = '\0';
				if(strcasecmp(method,"POST")==0)
				{
						printf("cccccccccccc\n");
						for(;i < content_len;++i)
						{
								recv(sock,&c,1,0);
								write(cgi_input[1],&c,1);
						}
				}
				printf("\n");

				while(read(cgi_output[0],&c,1) > 0)
				{
						send(sock,&c,1,0);
				}

				waitpid(id,NULL,0);
				close(cgi_input[1]);
				close(cgi_output[0]);

		}
}