コード例 #1
0
ファイル: http_write_utils.c プロジェクト: bboozzoo/zephyr
int http_response_soft_404(struct http_server_ctx *ctx)
{
	return http_response(ctx, HTTP_STATUS_200_OK, HTML_HEADER
			     "<h2><center>404 Not Found</center></h2>"
			     HTML_FOOTER);
}
コード例 #2
0
ファイル: HttpResponse.cpp プロジェクト: huangyt/MyProjects
		HttpResponse::p HttpResponse::ParseFromBufferMini(string response, size_t& http_header_length)
		{
			http_header_length = response.find("\r\n\r\n");
			if( string::npos ==  http_header_length)
			{	// HttpRequest 中不可能包含
				return HttpResponse::p();
			}
			http_header_length += 4;
			HttpResponse::p http_response(new HttpResponse);

			http_response->response_header_string_ = response;

			// 将 string 分解成一行一行
			vector<string> lines;
			string first_line = "";
			lines = framework::util::splite(response, "\r\n");

			// 遍历一行一行,对每一行做分析
			for(vector<string>::iterator iter = lines.begin(); iter != lines.end(); iter ++)
			{
				string line = *iter;
				if( boost::algorithm::trim_copy(line).empty() )
					continue;
				if( first_line == "" )
				{
					first_line = line;
					continue;
				}
				vector<string> key_value;
				key_value = framework::util::splite(line, ": ");
				if( key_value.size() != 2 ) continue;
				string key = key_value[0];		boost::algorithm::trim(key);
				string val = key_value[1];		boost::algorithm::trim(val);
				if( boost::algorithm::to_lower_copy(key) != "pragma" ) 
				{
					if( http_response->properties_.find(key) != http_response->properties_.end() )
					{
						continue;
					}
					http_response->properties_[key] = val;
				}
				else
				{
					vector<string> pragma_key_value;
					boost::algorithm::split(pragma_key_value, line, boost::algorithm::is_any_of("="));
					if( pragma_key_value.size() != 2 ) continue;
					string pragma_key = pragma_key_value[0];		boost::algorithm::trim(pragma_key);
					string pragma_val = pragma_key_value[1];		boost::algorithm::trim(pragma_val);
					if( http_response->pragmas_.find(pragma_key) != http_response->pragmas_.end() )
					{
						continue;
					}
					http_response->pragmas_[pragma_key] = pragma_val;
				}
			}

			// 分析 第一行
			vector<string> str_s;
			boost::algorithm::split(str_s, first_line, boost::algorithm::is_any_of(" "));
			if( str_s.size() < 3 )
			{
				return HttpResponse::p();
			}
			boost::algorithm::trim(str_s[0]);
			boost::algorithm::to_upper(str_s[0]);
			if( ! boost::algorithm::starts_with( str_s[0], "HTTP") )
			{
				return HttpResponse::p();
			}
			try
			{
				http_response->status_code_	= boost::lexical_cast<u_int>(str_s[1]);
			}
			catch (...) 
			{
				return HttpResponse::p();
			}
			for(size_t i = 2; i < str_s.size(); i ++)
				http_response->status_string_.append(str_s[i] + " ");
			boost::algorithm::trim(http_response->status_string_);

			return http_response;
		}
コード例 #3
0
ファイル: upload.c プロジェクト: 1514louluo/kore
int
page(struct http_request *req)
{
	int			fd;
	struct http_file	*file;
	u_int8_t		buf[BUFSIZ];
	ssize_t			ret, written;

	/* Only deal with POSTs. */
	if (req->method != HTTP_METHOD_POST) {
		http_response(req, 405, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* Parse the multipart data that was present. */
	http_populate_multipart_form(req);

	/* Find our file. */
	if ((file = http_file_lookup(req, "file")) == NULL) {
		http_response(req, 400, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* Open dump file where we will write file contents. */
	fd = open(file->filename, O_CREAT | O_TRUNC | O_WRONLY, 0700);
	if (fd == -1) {
		http_response(req, 500, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* While we have data from http_file_read(), write it. */
	/* Alternatively you could look at file->offset and file->length. */
	ret = KORE_RESULT_ERROR;
	for (;;) {
		ret = http_file_read(file, buf, sizeof(buf));
		if (ret == -1) {
			kore_log(LOG_ERR, "failed to read from file");
			http_response(req, 500, NULL, 0);
			goto cleanup;
		}

		if (ret == 0)
			break;

		written = write(fd, buf, ret);
		if (written == -1) {
			kore_log(LOG_ERR,"write(%s): %s",
			    file->filename, errno_s);
			http_response(req, 500, NULL, 0);
			goto cleanup;
		}

		if (written != ret) {
			kore_log(LOG_ERR, "partial write on %s",
			    file->filename);
			http_response(req, 500, NULL, 0);
			goto cleanup;
		}
	}

	ret = KORE_RESULT_OK;
	http_response(req, 200, NULL, 0);
	kore_log(LOG_INFO, "file '%s' successfully received",
	    file->filename);

cleanup:
	if (close(fd) == -1)
		kore_log(LOG_WARNING, "close(%s): %s", file->filename, errno_s);

	if (ret == KORE_RESULT_ERROR) {
		if (unlink(file->filename) == -1) {
			kore_log(LOG_WARNING, "unlink(%s): %s",
			    file->filename, errno_s);
		}
		ret = KORE_RESULT_OK;
	}

	return (KORE_RESULT_OK);
}
コード例 #4
0
ファイル: connection.c プロジェクト: xf22001/homeserver
static void work_http( connection* conn )
{
	int i;
	virtual_host * host;
	//check if the connection cannot work.
	if( conn->state != C_READY )
		return;
	conn->state = C_REQUESTING;
	http_request( conn );
	if( conn->state != C_REQUESTING )
		return;
	//response
	conn->code = 200;
	conn->state = C_RESPONSING;
	/* Check Host and then set root directory. */
	host = loop_search( &conn->server->loop_vhost, conn->host, vhost_searcher );
	if( !host ){
		host = loop_search( &conn->server->loop_vhost, "*", vhost_searcher );
	}
	if( host ){
		//read root
		conn->root_dir = host->root_dir;
		if( !loop_is_empty( &host->loop_rewrite ) )
			loop_search( &host->loop_rewrite, (void*)conn, loop_rewrite_match );
		http_parse_uri( conn );
		DBG("[%s]%s%s", conn->client->ip_string, conn->host, conn->uri );
_RESPONSE:	
		http_parse_path( conn );
		conn->document_type = http_doctype( conn->server, conn->extension );
		if( !conn->document_type ){
			http_error( conn, 404, "<h1>File not found.</h1>" );
		}else if( conn->extension[0] &&
			strstr( conn->server->asp_exts, conn->extension ) )
		{
			//php  do ...
			exec_asp( conn );
		}else if( host->proxy && ( !host->proxy_exts[0] ||
			strstr( host->proxy_exts, conn->extension ) ) )
		{
			// uses proxy server
			proxy_request( conn, host->proxy_ip, host->proxy_port );
		}else if( access(conn->full_path, 0)==0 ){
			if( is_dir(conn->full_path) ){
				char* tmp;
				NEW( tmp, PATH_LEN+32 );
				if( conn->script_name[strlen(conn->script_name)-1] != '/' ){
					//Are you sure that script starts with '/'?
					sprintf( tmp, "http://%s%s/", conn->host, conn->script_name );  
					http_redirect( conn, tmp );
				}else{
					if( tmp ){
						for( i = 0; i<10; i++ )
						{
							if( !conn->server->default_pages[i][0] ) {
								i=10;
								break;
							}
							sprintf( tmp, "%s/%s", conn->full_path, conn->server->default_pages[i] );
							if( access( tmp, 0 ) == 0 )
							{
								//091004 by Huang Guan.
								sprintf( conn->script_name, "%s%s", conn->script_name, 
									conn->server->default_pages[i] );
								DEL( tmp );
								goto _RESPONSE;
							}
						}
					}
					if( i == 10 ){
						// List Directory
						if( host->list ){
							int ret;
							NEW( conn->data_send, MAX_DATASEND+4 );
							strcpy( conn->extension, "html" );
							conn->document_type = http_doctype( conn->server, conn->extension );
							ret = listdir( conn->data_send, MAX_DATASEND, conn->full_path, 
								conn->script_name );
							conn->data_size = ret;
						}else{
							http_error( conn, 403, "<h1>Forbidden</h1>" );
						}
					}
				}
				DEL( tmp );
			}else{
				http_sendfile( conn, conn->full_path );
			}
		}else if( strncmp(conn->current_dir, "/system", 7)==0 && conn->root_dir==host->root_dir ){
			strcpy(conn->script_name, conn->script_name+7);
			conn->root_dir = conn->client->server->root_dir;
			goto _RESPONSE;
		}else{
			http_error( conn, 404, "<h1>File not found.</h1>" );
		}
	}else{
		http_error( conn, 403, "<h1>Unknown host name.</h1>" );
	}

	if( conn->state == C_RESPONSING )
		http_response( conn );
	conn->requests ++;
	if( conn->form_data )
		DEL( conn->form_data );
	if( conn->data_send )
		DEL( conn->data_send );
	
	if( conn->session )
		conn->session->reference --;
	conn->session = NULL;
		
	//next request
	if( conn->keep_alive ){
		conn->state = C_READY;
	}else{
		conn->state = C_END;
	}
}