Auto merge of #10953 - KiChjang:text-plain-encoding, r=jdm

Implement text/plain form encoding

Fixes #3649.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10953)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-05-11 19:43:37 -07:00
commit 641b374f0b
3 changed files with 67 additions and 7 deletions

View file

@ -270,9 +270,9 @@ impl HTMLFormElement {
}
// https://html.spec.whatwg.org/multipage/#multipart/form-data-encoding-algorithm
fn encode_form_data(&self, form_data: &mut Vec<FormDatum>,
encoding: Option<EncodingRef>,
boundary: String) -> String {
fn encode_multipart_form_data(&self, form_data: &mut Vec<FormDatum>,
encoding: Option<EncodingRef>,
boundary: String) -> String {
// Step 1
let mut result = "".to_owned();
@ -321,7 +321,37 @@ impl HTMLFormElement {
result.push_str(&*format!("\r\n--{}--", boundary));
return result;
result
}
// https://html.spec.whatwg.org/multipage/#text/plain-encoding-algorithm
fn encode_plaintext(&self, form_data: &mut Vec<FormDatum>) -> String {
// Step 1
let mut result = String::new();
// Step 2
let encoding = self.pick_encoding();
// Step 3
let charset = &*encoding.whatwg_name().unwrap();
for entry in form_data.iter_mut() {
// Step 4
if entry.name == "_charset_" && entry.ty == "hidden" {
entry.value = FormDatumValue::String(DOMString::from(charset.clone()));
}
// Step 5
if entry.ty == "file" {
entry.value = FormDatumValue::String(DOMString::from(entry.value_str()));
}
// Step 6
result.push_str(&*format!("{}={}\r\n", entry.name, entry.value_str()));
}
// Step 7
result
}
/// [Form submission](https://html.spec.whatwg.org/multipage/#concept-form-submit)
@ -377,6 +407,7 @@ impl HTMLFormElement {
load_data.headers.set(ContentType::form_url_encoded());
form_urlencoded::Serializer::new(String::new())
.encoding_override(Some(self.pick_encoding()))
.extend_pairs(form_data.into_iter().map(|field| (field.name.clone(), field.value_str())))
.finish()
}
@ -385,10 +416,13 @@ impl HTMLFormElement {
let mime = mime!(Multipart / FormData; Boundary =(&boundary));
load_data.headers.set(ContentType(mime));
self.encode_form_data(&mut form_data, None, boundary)
self.encode_multipart_form_data(&mut form_data, None, boundary)
}
FormEncType::TextPlainEncoded => {
load_data.headers.set(ContentType(mime!(Text / Plain)));
self.encode_plaintext(&mut form_data)
}
// TODO: Support plain text encoding
FormEncType::TextPlainEncoded => "".to_owned()
};
// Step 18

View file

@ -4,6 +4,11 @@ def main(request, response):
return 'OK'
else:
return 'FAIL'
elif request.headers.get('Content-Type') == 'text/plain':
if request.body == 'qux=baz\r\n':
return 'OK'
else:
return 'FAIL'
else:
if request.POST.first('foo') == 'bar':
return 'OK'

View file

@ -18,6 +18,13 @@ var simple_tests = [
submitelement: "",
submitaction: function(doc) { doc.getElementById("testform").submit(); }
},
{
name: "form submission from form should navigate to url with text/plain",
input: "<textarea name=qux>baz</textarea>",
enctype: "text/plain",
submitelement: "",
submitaction: function(doc) { doc.getElementById("testform").submit(); }
},
{
name: "form submission from button should navigate to url with x-www-form-urlencoded",
input: "<input name=foo value=bara>",
@ -32,6 +39,13 @@ var simple_tests = [
submitelement: "<button id=buttonsubmit type=\"submit\">Submit</button>",
submitaction: function(doc) { doc.getElementById("buttonsubmit").click(); }
},
{
name: "form submission from button should navigate to url with text/plain",
input: "<textarea name=qux>baz</textarea>",
enctype: "text/plain",
submitelement: "<button id=buttonsubmit type=\"submit\">Submit</button>",
submitaction: function(doc) { doc.getElementById("buttonsubmit").click(); }
},
{
name: "form submission from input should navigate to url with x-www-form-urlencoded",
input: "<input name=foo value=bara>",
@ -46,6 +60,13 @@ var simple_tests = [
submitelement: "<input id=inputsubmit type=\"submit\">Submit</input>",
submitaction: function(doc) { doc.getElementById("inputsubmit").click(); }
},
{
name: "form submission from input should navigate to url with text/plain",
input: "<input name=qux value=baz>",
enctype: "text/plain",
submitelement: "<input id=inputsubmit type=\"submit\">Submit</input>",
submitaction: function(doc) { doc.getElementById("inputsubmit").click(); }
}
];
simple_tests.forEach(function(test_obj) {
test_obj.test = async_test(test_obj.name);