Repository: servo/servo
File: components/script/dom/html/htmlformelement.rs (Rust)

Code before:
        // Step 1.1: Perform newline replacement on entry's name
        entry.name = clean_crlf(&entry.name);

        // Step 1.2: If entry's value is not a File object, perform newline replacement on entry's
        // value
        if let FormDatumValue::String(ref s) = entry.value {
            entry.value = FormDatumValue::String(clean_crlf(s));
        }

        // Step 2: Return the byte sequence resulting from encoding the entry list.
        // https://tools.ietf.org/html/rfc7578#section-4
        // NOTE(izgzhen): The encoding here expected by most servers seems different from
        // what spec says (that it should start with a '\r\n').
        let mut boundary_bytes = format!("--{}\r\n", boundary).into_bytes();
        result.append(&mut boundary_bytes);

        // TODO(eijebong): Everthing related to content-disposition it to redo once typed headers
        // are capable of it.
        match entry.value {
            FormDatumValue::String(ref s) => {
                let content_disposition = format!("form-data; name=\"{}\"", entry.name);
                let mut bytes = format!(
                    "Content-Disposition: {}\r\n\r\n{}\r\n",
                    content_disposition, s
                )
                .into_bytes();
                result.append(&mut bytes);
            },
            FormDatumValue::File(ref f) => {
                let charset = encoding.name();
                let extra = if charset.to_lowercase() == "utf-8" {
                    format!("filename=\"{}\"", String::from(f.name().str()))
                } else {
                    format!(
                        "filename*=\"{}\"''{}",
                        charset,
                        http_percent_encode(&f.name().as_bytes())
                    )
                };

                let content_disposition = format!("form-data; name=\"{}\"; {}", entry.name, extra);
                // https://tools.ietf.org/html/rfc7578#section-4.4
                let content_type: Mime = f
                    .upcast::<Blob>()
                    .Type()
                    .parse()
                    .unwrap_or(mime::TEXT_PLAIN);
                let mut type_bytes = format!(
                    "Content-Disposition: {}\r\ncontent-type: {}\r\n\r\n",
                    content_disposition, content_type
                )

Review comment (suggestion):
Could be a tiny bit shorter and more readable:

```suggestion
FormDatumValue::String(ref value) => {
let content_disposition = format!("form-data; name=\"{}\"", entry.name);
                let mut bytes = format!(
                    "Content-Disposition: {content_disposition}\r\n\r\n{value}\r\n")
                .into_bytes();
```

Code after:
        // Step 1.1: Perform newline replacement on entry's name
        entry.name = clean_crlf(&entry.name);

        // Step 1.2: If entry's value is not a File object, perform newline replacement on entry's
        // value
        if let FormDatumValue::String(ref s) = entry.value {
            entry.value = FormDatumValue::String(clean_crlf(s));
        }

        // Step 2: Return the byte sequence resulting from encoding the entry list.
        // https://tools.ietf.org/html/rfc7578#section-4
        // NOTE(izgzhen): The encoding here expected by most servers seems different from
        // what spec says (that it should start with a '\r\n').
        let mut boundary_bytes = format!("--{}\r\n", boundary).into_bytes();
        result.append(&mut boundary_bytes);

        // TODO(eijebong): Everthing related to content-disposition it to redo once typed headers
        // are capable of it.
        match entry.value {
            FormDatumValue::String(ref s) => {
                let content_disposition = format!("form-data; name=\"{}\"", entry.name);
                let mut bytes =
                    format!("Content-Disposition: {content_disposition}\r\n\r\n{s}\r\n",)
                        .into_bytes();
                result.append(&mut bytes);
            },
            FormDatumValue::File(ref f) => {
                let charset = encoding.name();
                let extra = if charset.to_lowercase() == "utf-8" {
                    format!("filename=\"{}\"", String::from(f.name().str()))
                } else {
                    format!(
                        "filename*=\"{}\"''{}",
                        charset,
                        http_percent_encode(&f.name().as_bytes())
                    )
                };

                let content_disposition = format!("form-data; name=\"{}\"; {}", entry.name, extra);
                // https://tools.ietf.org/html/rfc7578#section-4.4
                let content_type: Mime = f
                    .upcast::<Blob>()
                    .Type()
                    .parse()
                    .unwrap_or(mime::TEXT_PLAIN);
                let mut type_bytes = format!(
                    "Content-Disposition: {}\r\ncontent-type: {}\r\n\r\n",
                    content_disposition, content_type
                )
                .into_bytes();
                result.append(&mut type_bytes);