|
1 | 1 | use std::ops::{Deref, DerefMut};
|
2 |
| -use std::io::Read; |
| 2 | +use std::io::{self, Read}; |
3 | 3 |
|
4 | 4 | use rocket::outcome::{Outcome, IntoOutcome};
|
5 | 5 | use rocket::request::Request;
|
@@ -88,18 +88,23 @@ impl<T> Json<T> {
|
88 | 88 | const LIMIT: u64 = 1 << 20;
|
89 | 89 |
|
90 | 90 | impl<T: DeserializeOwned> FromData for Json<T> {
|
91 |
| - type Error = SerdeError; |
| 91 | + type Error = io::Error; |
92 | 92 |
|
93 |
| - fn from_data(request: &Request, data: Data) -> data::Outcome<Self, SerdeError> { |
| 93 | + fn from_data(request: &Request, data: Data) -> data::Outcome<Self, io::Error> { |
94 | 94 | if !request.content_type().map_or(false, |ct| ct.is_json()) {
|
95 | 95 | error_!("Content-Type is not JSON.");
|
96 | 96 | return Outcome::Forward(data);
|
97 | 97 | }
|
98 | 98 |
|
99 | 99 | let size_limit = request.limits().get("json").unwrap_or(LIMIT);
|
100 |
| - serde_json::from_reader(data.open().take(size_limit)) |
| 100 | + let mut buf = String::new(); |
| 101 | + if let Err(e) = data.open().take(size_limit).read_to_string(&mut buf) { |
| 102 | + error_!("IO Error: {:?}", e); |
| 103 | + return Outcome::Failure((Status::BadRequest, e)); |
| 104 | + } |
| 105 | + serde_json::from_str(&buf) |
101 | 106 | .map(|val| Json(val))
|
102 |
| - .map_err(|e| { error_!("Couldn't parse JSON body: {:?}", e); e }) |
| 107 | + .map_err(|e| { error_!("Couldn't parse JSON body: {:?}", e); e.into() }) |
103 | 108 | .into_outcome(Status::BadRequest)
|
104 | 109 | }
|
105 | 110 | }
|
|
0 commit comments