Пример #1
0
void main(int argc, char **argv)
{
    char uri[MAX_STR_LEN];
    char hostname[MAX_STR_LEN];
    char identifier[MAX_STR_LEN]="";
    char ip[100];
    int sockid, port;
    //printf("Open URI: ");
    //scanf("%s", uri);
    //printf("Testing Begin...n");

    //test for parse
    if(parse_URI(argv[1], hostname, &port, identifier) == 0)
    {
        printf("Errorr in Parasingn ");
        exit(1);
    }
    
    //get the ip address
    if(hostname_to_ip(hostname, ip))
    {
        printf("Error resolving: %s\n", hostname);
        exit(1);
    }
    printf("port----->%d\n",port);

    // gernerate socket id
    sockid = open_connection(hostname, port, ip, identifier);

    // send and receive message
    perform_http(sockid, identifier, hostname);
}
Пример #2
0
int main(int argc, char **argv)
{
    signal(SIGINT, interupt_handler);

    char uri[MAX_STR_LEN];
    char hostname[MAX_STR_LEN];
    char identifier[MAX_STR_LEN];
    int port;

    if (argc != 1) {
      strcpy(uri, argv[1]);
    } else {
      puts("ERROR: SimpClient requires a uri as a arguement.\n");
      exit(1);
    }

    printf("\nOpenning URI: %s\n", uri);

    parse_URI(uri, hostname, &port, identifier);

    printf("Hostname: %s port: %d identifier: %s\n", hostname, port, identifier);

    sockid = open_connection(hostname, port);
    
    printf("Hostname: %s\nport: %d\nidentifier: %s\n", hostname, port, identifier);

    perform_http(sockid, hostname, identifier);

    return 0;
}
Пример #3
0
main(int argc, char *argv)
{
    char uri[MAX_STR_LEN];
    char hostname[MAX_STR_LEN];
    char identifier[MAX_STR_LEN];
    int sockid, port;

    printf("Open URI:  ");
    scanf("%s", uri);

    parse_URI(uri, hostname, &port, identifier);
    /*sockid = open_connection(hostname, port);
    perform_http(sockid, identifier);*/
}
Пример #4
0
int parse_http_header(char * data, char * ident) {
    
    char hostname[MAX_STR_LEN];
    char identifier[MAX_STR_LEN];
    char uri[MAX_STR_LEN];
    int port;
    
    DEBUG_PRINT(("\nparse_http_header: ident: [%s]", ident));
    
    
    char * token; 
    printf("DATA[%s]", data);
    token = strtok(data, "\n\r ");
    if(token==NULL) return 5;
    
    printf("TOKEN[%s]", token);
    if(strcmp("GET",token) != 0) {
        return 3;
    }
    
    
    token = strtok(NULL, "\n\r ");
    int parser_exit_code = parse_URI(token, hostname, &port, identifier);
    if(parser_exit_code == 0) {
        DEBUG_PRINT(("\n\nParser Failed\n"));
        return 1;
    }

    char tmp[MAX_STR_LEN];
    sprintf(tmp,"%s%s", ident, identifier);
    DEBUG_PRINT(("file_path: [%s]",tmp));
    strcpy(ident, tmp);
    if( access( tmp, F_OK ) == -1 ) return 404;
    
    
    token = strtok(NULL, "\n\r ");
    DEBUG_PRINT(("token: [%s]", token));
    if(strcmp("HTTP/1.0", token) != 0) {
        return 2;
    }
    
    
    return 200;
}
main(int argc, char *argv)
{
	char uri[MAX_STR_LEN];
	char hostname[MAX_STR_LEN];
	char identifier[MAX_STR_LEN];
	int sockid, port;

	// Read input from the terminal, and extract information from URI for an HTTP request
    	printf("Open URI:  ");
    	scanf("%s", uri);
    	parse_URI(uri, hostname, &port, identifier);
	printf("\n--- REQUEST ---\n%s\nHost: %s\nPort: %d\n---------------\n", identifier, hostname, port);
	strcat(identifier,"\r\n\r\n");

	// Open socket connection, and connect to server
	sockid = open_connection(hostname, port);

	// Send an HTTP request to the server
    	perform_http(sockid, identifier);
}
Пример #6
0
/*---------------------------------------------------------------------------*
 *
 * Accepts a request from "sockid" and sends a response to "sockid".
 *
 *---------------------------------------------------------------------------*/
perform_http(int sockid) {

	char str_rec_buff[MAX_REC_LEN]; bzero( str_rec_buff, MAX_REC_LEN);
	char str_send_buff[MAX_SEND_LEN]; bzero( str_send_buff, MAX_SEND_LEN);
	FILE *file_to_send;
	
	//Read from the socket
	if ((readn(sockid,str_rec_buff,MAX_REC_LEN)) < 0){
		printf("Error reading TCP stream from client\n");
		close(sockid);
		cleanExit();
	}

	//Parse received string;
	char method[MAX_REC_LEN];
	char request_URI[MAX_REC_LEN];
	char http_version[MAX_REC_LEN];
	sscanf(str_rec_buff, "%s %s %s\r\n", method, request_URI, http_version);
	
	//Parse the URI for the requested file
	char hostname[MAX_REC_LEN];
	char identifier[MAX_REC_LEN];
	int port;
	parse_URI(request_URI, hostname, &port, identifier, MAX_REC_LEN);
	
	//Ensure the GET method is the one being asked for
	if ((strcmp(method,"GET") != 0) || (strcmp(http_version,"HTTP/1.0")) ){
		writen(sockid, "HTTP/1.0 501 Not Implemented\r\n\r\n", MAX_SEND_LEN);
		return;
	}

	//Get System Information
	struct utsname unameData;
	uname(&unameData);
	//Get System Time
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	
	//Setup the strings that gives us the file location and the system info
	char file_location[2*MAX_REC_LEN];
	snprintf(file_location, MAX_REC_LEN*2, "%s/%s",directory,identifier);
	char server_info[MAX_SEND_LEN];
	snprintf(server_info, MAX_REC_LEN, "Date: %sServer: %s %s %s\r\n\r\n", asctime(timeinfo), unameData.sysname, unameData.nodename, unameData.release);

	//HTTP Header is built and sent here if file is found
	if ((file_to_send = fopen(file_location,"r")) != NULL) {
		strcpy(str_send_buff, "HTTP/1.0 200 OK\r\n");
		strcat(str_send_buff, server_info);
		int http_len = strlen(str_send_buff);
		fread(str_send_buff+http_len, 1, MAX_SEND_LEN-http_len, (FILE*)file_to_send);
		writen(sockid, str_send_buff, MAX_SEND_LEN);
		fclose(file_to_send);
	}else {
		strcpy(str_send_buff, "HTTP/1.0 404 Not Found");
		strcat(str_send_buff, server_info);
		writen(sockid, str_send_buff, MAX_SEND_LEN);
		return;
	}

}