Compare commits
	
		
			No commits in common. "master" and "v0.1.5" have entirely different histories.
		
	
	
		
	
		
| @ -1,6 +1,6 @@ | |||||||
| [package] | [package] | ||||||
| name = "http" | name = "http" | ||||||
| version = "0.1.6" | version = "0.1.5" | ||||||
| edition = "2021" | edition = "2021" | ||||||
| 
 | 
 | ||||||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | # 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 | # http | ||||||
| 
 | 
 | ||||||
| A basic Rust library to parse an HTTP request and build HTTP response. | A basic Rust lib 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 |  | ||||||
| ``` |  | ||||||
| 
 |  | ||||||
|  | |||||||
| @ -7,8 +7,8 @@ pub struct HTTPBody { | |||||||
|     data: JsonValue, |     data: JsonValue, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// HTTPBody represents an HTTP request body.
 | /// HTTPBody represents an HTTP request body
 | ||||||
| /// For simplicity, only JSON body is allowed.
 | /// for simplicity, only JSON body is allowed
 | ||||||
| impl HTTPBody { | impl HTTPBody { | ||||||
|     fn new(data: JsonValue) -> HTTPBody { |     fn new(data: JsonValue) -> HTTPBody { | ||||||
|         HTTPBody { data } |         HTTPBody { data } | ||||||
|  | |||||||
							
								
								
									
										12
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								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.
 | //! * Only json body allowed
 | ||||||
| //!
 |  | ||||||
| //! NOTE: only few parts of the specification has been implemented.
 |  | ||||||
| //!
 |  | ||||||
| //! * Only JSON body allowed
 |  | ||||||
| //! * HTTP Headers not parsed
 | //! * HTTP Headers not parsed
 | ||||||
| 
 | 
 | ||||||
| mod body; | mod body; | ||||||
|  | |||||||
| @ -36,7 +36,7 @@ impl JSONMessage { | |||||||
|         self.message.insert(key.to_string(), value.to_string()); |         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> { |     pub fn error(message: &str) -> Option<JsonValue> { | ||||||
|         let mut http_message = JSONMessage::default(); |         let mut http_message = JSONMessage::default(); | ||||||
|         http_message.put("error", message); |         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 { |     fn build_json(self) -> String { | ||||||
|         let unstruct: Vec<String> = self |         let unstruct: Vec<String> = self | ||||||
|             .message |             .message | ||||||
|  | |||||||
| @ -16,14 +16,14 @@ pub struct HTTPRequest<'a> { | |||||||
| 
 | 
 | ||||||
| impl<'a> HTTPRequest<'a> { | impl<'a> HTTPRequest<'a> { | ||||||
|     /// get_request_parts splits correctly the incoming request in order to get:
 |     /// get_request_parts splits correctly the incoming request in order to get:
 | ||||||
|     /// * start line
 |     /// * start_line
 | ||||||
|     /// * headers
 |     /// * headers
 | ||||||
|     /// * data (if exists)
 |     /// * data (if exists)
 | ||||||
|     fn get_request_parts(request: &str) -> Result<RequestParts, &str> { |     fn get_request_parts(request: &str) -> Result<RequestParts, &str> { | ||||||
|         let mut request_parts: VecDeque<&str> = request.split(HTTP_REQUEST_SEPARATOR).collect(); |         let mut request_parts: VecDeque<&str> = request.split(HTTP_REQUEST_SEPARATOR).collect(); | ||||||
| 
 | 
 | ||||||
|         if request_parts.len() < 3 { |         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(); |         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> { |     pub fn get_body_value(&self, key: &str) -> Option<String> { | ||||||
|         match self.body { |         match self.body { | ||||||
|             Some(ref b) => match &b.get_data() { |             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"}"#; | const STATUS_OK: &'static str = r#"{"status":"ok"}"#; | ||||||
| 
 | 
 | ||||||
| /// HTTPResponse represents an HTTP response (headers are not parsed)
 | /// HTTPResponse represents an HTTP response (headers are not parsed)
 | ||||||
| ///
 |  | ||||||
| /// NOTE: for simplicity, only JSON body are allowed
 | /// NOTE: for simplicity, only JSON body are allowed
 | ||||||
| pub struct HTTPResponse { | pub struct HTTPResponse { | ||||||
|     status_line: HTTPStatusLine, |     status_line: HTTPStatusLine, | ||||||
|  | |||||||
| @ -21,6 +21,7 @@ impl Into<String> for HTTPStatusCode { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| pub struct HTTPStatusLine { | pub struct HTTPStatusLine { | ||||||
|     version: HTTPVersion, |     version: HTTPVersion, | ||||||
|     status_code: HTTPStatusCode, |     status_code: HTTPStatusCode, | ||||||
| @ -52,3 +53,4 @@ impl HTTPStatusLine { | |||||||
|         self.status_code = code; |         self.status_code = code; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user