Compare commits
	
		
			No commits in common. "master" and "v0.1.3" have entirely different histories.
		
	
	
		
	
		
| @ -1,6 +1,6 @@ | ||||
| [package] | ||||
| name = "http" | ||||
| version = "0.1.6" | ||||
| version = "0.1.3" | ||||
| edition = "2021" | ||||
| 
 | ||||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||||
|  | ||||
							
								
								
									
										26
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								README.md
									
									
									
									
									
								
							| @ -1,27 +1,3 @@ | ||||
| # http | ||||
| 
 | ||||
| A basic Rust library to parse an HTTP request and build HTTP response. | ||||
| 
 | ||||
| **NOTE**: only few parts of the specification has been implemented and only JSON body are allowed. | ||||
| 
 | ||||
| ## Integration | ||||
| Get the latest version: | ||||
| ```toml | ||||
| http = { git = "https://gitea.thegux.fr/rmanach/http" } | ||||
| ``` | ||||
| 
 | ||||
| Or get a specific version: | ||||
| ```toml | ||||
| http = { git = "https://gitea.thegux.fr/rmanach/http", version = "0.1.2" } | ||||
| ``` | ||||
| 
 | ||||
| ## Documentation | ||||
| ```bash | ||||
| cargo doc -r --no-deps --open | ||||
| ``` | ||||
| 
 | ||||
| ## Launch unit tests | ||||
| ```bash | ||||
| cargo test | ||||
| ``` | ||||
| 
 | ||||
| A basic Rust lib to parse an HTTP request and build HTTP response. | ||||
|  | ||||
| @ -7,8 +7,8 @@ pub struct HTTPBody { | ||||
|     data: JsonValue, | ||||
| } | ||||
| 
 | ||||
| /// HTTPBody represents an HTTP request body.
 | ||||
| /// For simplicity, only JSON body is allowed.
 | ||||
| /// HTTPBody represents an HTTP request body
 | ||||
| /// for simplicity, only JSON body is allowed
 | ||||
| impl HTTPBody { | ||||
|     fn new(data: JsonValue) -> HTTPBody { | ||||
|         HTTPBody { data } | ||||
|  | ||||
							
								
								
									
										14
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/lib.rs
									
									
									
									
									
								
							| @ -1,12 +1,10 @@ | ||||
| //! **http** library is a light HTTP parser and builder.
 | ||||
| //! http parses the request according to the HTTP message specifications
 | ||||
| //! it also includes `HTTPResponse` to build an HTTPResponse
 | ||||
| //!
 | ||||
| //! It parses the request according to the HTTP message specifications and includes `HTTPResponse` to build an HTTP Response.
 | ||||
| //! see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages
 | ||||
| //! NOTE: only few parts of the specification has been implemented
 | ||||
| //!
 | ||||
| //! See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages> for more details.
 | ||||
| //!
 | ||||
| //! NOTE: only few parts of the specification has been implemented.
 | ||||
| //!
 | ||||
| //! * Only JSON body allowed
 | ||||
| //! * Only json body allowed
 | ||||
| //! * HTTP Headers not parsed
 | ||||
| 
 | ||||
| mod body; | ||||
| @ -19,7 +17,7 @@ mod version; | ||||
| 
 | ||||
| pub use body::HTTPBody; | ||||
| pub use message::JSONMessage; | ||||
| pub use request::HTTPRequest; | ||||
| pub use request::{HTTPRequest, extract_json_value}; | ||||
| pub use response::HTTPResponse; | ||||
| pub use start_line::HTTPStartLine; | ||||
| pub use status::{HTTPStatusCode, HTTPStatusLine}; | ||||
|  | ||||
| @ -36,7 +36,7 @@ impl JSONMessage { | ||||
|         self.message.insert(key.to_string(), value.to_string()); | ||||
|     } | ||||
| 
 | ||||
|     // associated function to build an HTTPMessage error
 | ||||
|     /// associated function to build an HTTPMessage error
 | ||||
|     pub fn error(message: &str) -> Option<JsonValue> { | ||||
|         let mut http_message = JSONMessage::default(); | ||||
|         http_message.put("error", message); | ||||
| @ -54,7 +54,7 @@ impl JSONMessage { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /// build_json loops over all the HashMap keys, builds a JSON key value for each one and join them with `JSON_DELIMITER`
 | ||||
|     /// loops over all the HashMap keys, builds a JSON key value for each one and join them with `JSON_DELIMITER`
 | ||||
|     fn build_json(self) -> String { | ||||
|         let unstruct: Vec<String> = self | ||||
|             .message | ||||
|  | ||||
| @ -16,14 +16,14 @@ pub struct HTTPRequest<'a> { | ||||
| 
 | ||||
| impl<'a> HTTPRequest<'a> { | ||||
|     /// get_request_parts splits correctly the incoming request in order to get:
 | ||||
|     /// * start line
 | ||||
|     /// * start_line
 | ||||
|     /// * headers
 | ||||
|     /// * data (if exists)
 | ||||
|     fn get_request_parts(request: &str) -> Result<RequestParts, &str> { | ||||
|         let mut request_parts: VecDeque<&str> = request.split(HTTP_REQUEST_SEPARATOR).collect(); | ||||
| 
 | ||||
|         if request_parts.len() < 3 { | ||||
|             return Err("request has not enough informations to be correctly parsed"); | ||||
|             return Err("request has no enough informations to be correctly parsed"); | ||||
|         } | ||||
| 
 | ||||
|         let start_line = request_parts.pop_front().unwrap(); | ||||
| @ -55,7 +55,7 @@ impl<'a> HTTPRequest<'a> { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /// get_body_value retrieves JSON value in `HTTPBody`
 | ||||
|     /// get_bodyèvalue retrieves JSON value in `HTTPBody`
 | ||||
|     pub fn get_body_value(&self, key: &str) -> Option<String> { | ||||
|         match self.body { | ||||
|             Some(ref b) => match &b.get_data() { | ||||
|  | ||||
| @ -10,7 +10,6 @@ const BAD_REQUEST_ERROR: &'static str = r#"{"error":"bad request"}"#; | ||||
| const STATUS_OK: &'static str = r#"{"status":"ok"}"#; | ||||
| 
 | ||||
| /// HTTPResponse represents an HTTP response (headers are not parsed)
 | ||||
| ///
 | ||||
| /// NOTE: for simplicity, only JSON body are allowed
 | ||||
| pub struct HTTPResponse { | ||||
|     status_line: HTTPStatusLine, | ||||
| @ -34,16 +33,12 @@ impl Default for HTTPResponse { | ||||
|     fn default() -> Self { | ||||
|         HTTPResponse { | ||||
|             status_line: HTTPStatusLine::default(), | ||||
|             body: json::parse("{}").unwrap(), | ||||
|             body: json::parse(r#"{}"#).unwrap(), | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl HTTPResponse { | ||||
|     pub fn get_status_code(&self) -> HTTPStatusCode { | ||||
|         self.status_line.get_status_code() | ||||
|     } | ||||
| 
 | ||||
|     pub fn as_500(message: Option<json::JsonValue>) -> Self { | ||||
|         let mut response = Self::default(); | ||||
| 
 | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| use crate::HTTPVersion; | ||||
| 
 | ||||
| #[derive(Debug, PartialEq, Clone, Copy)] | ||||
| #[derive(Debug, PartialEq)] | ||||
| pub enum HTTPStatusCode { | ||||
|     Http200, | ||||
|     Http400, | ||||
| @ -44,10 +44,6 @@ impl Into<String> for HTTPStatusLine { | ||||
| } | ||||
| 
 | ||||
| impl HTTPStatusLine { | ||||
|     pub fn get_status_code(&self) -> HTTPStatusCode { | ||||
|         self.status_code | ||||
|     } | ||||
| 
 | ||||
|     pub fn set_status_code(&mut self, code: HTTPStatusCode) { | ||||
|         self.status_code = code; | ||||
|     } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user