Exemplo n.º 1
0
int http_session(int* connect_fd,struct sockaddr_in * client_addr)
{
	char recv_buf[RECV_BUFFER_SIZE+1];
	unsigned char send_buf[SEND_BUFFER_SIZE+1];
	unsigned char file_buf[FILE_MAX_SIZE+1];
	memset(recv_buf,'\0',sizeof(recv_buf));
	memset(send_buf,'\0',sizeof(send_buf));
	memset(file_buf,'\0',sizeof(file_buf));
	char uri_buf[URI_SIZE+1];
	memset(uri_buf,'\0',sizeof(uri_buf));
	int maxfd=*connect_fd+1;
	fd_set read_set;
	FD_ZERO(&read_set);
	struct timeval timeout;
	timeout.tv_sec=TIME_OUT_SEC;
	timeout.tv_usec=TIME_OUT_USEC;
	int flag=1;
	int res=0;
	int read_bytes=0;
	int send_bytes=0;
	int file_size=0;
	char * mime_type;
	int uri_status;
	FD_SET(*connect_fd,&read_set);
	while(flag)
	{
		res=select(maxfd,&read_set,NULL,NULL,&timeout);
		switch(res)
		{
			case -1:
				perror("select () error ,in http_session.c");
				close(*connect_fd);
				return -1;
				break;
			case 0:
				continue;
				break;
			default:
				if(FD_ISSET(*connect_fd,&read_set))
				{
					memset(recv_buf,'\0',sizeof(recv_buf));
					if((read_bytes=recv(*connect_fd,recv_buf,RECV_BUFFER_SIZE,0))==0)
					{
						return 0;
					}else if(read_bytes>0)
					{
						if(is_http_protocol(recv_buf)==0)
						{
							fprintf(stderr,"not httlp protocol \n");
							close(*connect_fd);
							return -1;
						}else
						{
							memset(uri_buf,'\0',sizeof(uri_buf));
							if(get_uri(recv_buf,uri_buf)==NULL)
							{
								uri_status=URI_TOO_LONG;
							}
							else
							{
								printf("URI:%s\n",uri_buf);
								uri_status=get_uri_status(uri_buf);
								switch(uri_status)
								{
									case FILE_OK:
										printf("file ok\n");
										mime_type=get_mime_type(uri_buf);
										printf("mime type :%s\n",mime_type);
										file_size=get_file_disk(uri_buf,file_buf);
										send_bytes=reply_normal_information(send_buf,file_buf,file_size,mime_type);
										break;

									case FILE_NOT_FOUND:
										printf("in switch on case FILE_NOT_FOUND\n");
										send_bytes=set_error_information(send_buf,FILE_NOT_FOUND);
										break;
									case FILE_FORBIDEN:
										break;
									default:
										break;

								}
								send(*connect_fd,send_buf,send_bytes,0);
							}
						}


					}
				}
		}
	}
	return 0;
}
Exemplo n.º 2
0
int http_session(int *connect_fd, struct sockaddr_in *client_addr)
{
    char recv_buf[RECV_BUFFER_SIZE + 1];            /* server socket receive buffer */
    unsigned char send_buf[SEND_BUFFER_SIZE + 1];    /* server socket send bufrer */
    unsigned char file_buf[FILE_MAX_SIZE + 1];
    memset(recv_buf, '\0', sizeof(recv_buf));
    memset(send_buf, '\0', sizeof(send_buf));
    memset(file_buf, '\0', sizeof(file_buf));

    char uri_buf[URI_SIZE + 1];                        /* store the the uri request from client */
    memset(uri_buf, '\0', sizeof(uri_buf));

    int maxfd = *connect_fd + 1;
    fd_set read_set;
    FD_ZERO(&read_set);

    struct timeval timeout;
    timeout.tv_sec = TIME_OUT_SEC;
    timeout.tv_usec = TIME_OUT_USEC;


    int flag = 1;
    int res = 0;
    int read_bytes = 0;
    int send_bytes = 0;
    int file_size = 0;
    char *mime_type;
    int uri_status;
    FD_SET(*connect_fd, &read_set);
    while(flag)
    {
        
        res = select(maxfd, &read_set, NULL, NULL, &timeout);
        switch(res)
        {
            case -1:
             perror("select() error. in http_sesseion.c");
             close(*connect_fd);
             return -1;
             break;
            case 0:            /* time out, continue to select */
             continue;
             break;
            default:        /* there are some file-descriptor's status changed */
             if(FD_ISSET(*connect_fd, &read_set))
             {
                memset(recv_buf, '\0', sizeof(recv_buf));
                if((read_bytes = recv(*connect_fd, recv_buf, RECV_BUFFER_SIZE, 0)) == 0)
                {
                    /* client close the connection */
                    return 0;
                }
                else if(read_bytes > 0)        /* there are some data from client */
                {
                    if(is_http_protocol(recv_buf) == 0)    /* check is it HTTP protocol */
                    {
                        fprintf(stderr, "Not http protocol.\n");
                        close(*connect_fd);
                        return -1;
                    }
                    else        /* http protocol */
                    {
                        memset(uri_buf, '\0', sizeof(uri_buf));
                        if(get_uri(recv_buf, uri_buf) == NULL)    /* get the uri from http request head */
                        {
                            uri_status = URI_TOO_LONG;

                        }
                        else
                        {
                            printf("URI:%s\n", uri_buf);
                            uri_status = get_uri_status(uri_buf);
                            switch(uri_status)
                            {
                                case FILE_OK:
                                 printf("file ok\n");
                                 mime_type = get_mime_type(uri_buf);
                                 printf("mime type: %s\n", mime_type);
                                 file_size = get_file_disk(uri_buf, file_buf);
                                 send_bytes = reply_normal_information(send_buf, file_buf, file_size, mime_type);
                                    
                        //         send(*connect_fd, send_buf, send_bytes, 0);


                                 break;
                                case FILE_NOT_FOUND:    /* file not found on server */
                                 printf("in switch on case FILE_NOT_FOUND\n");
                                 send_bytes = set_error_information(send_buf, FILE_NOT_FOUND);
                                 
                                 break;    
                                case FILE_FORBIDEN:        /* server have no permission to read the request file */
                                 break;
                                case URI_TOO_LONG:        /* the request uri is too long */

                                 break;
                                default:
                                 break;
                            }
                            
                         send(*connect_fd, send_buf, send_bytes, 0);
                        }
                    }
                }
             }

        }

    }

    return 0;
}