Skip to content

Calculate buffer length of utf-8 data for json correctly #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/bhttp.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,19 @@ preparePayload = (request, response, requestState) ->

if request.options.encodeJSON
debugRequest "... but encodeJSON was set, so we will send JSON instead"
request.options.headers["content-type"] = "application/json"
request.options.headers["content-type"] = "application/json; charset=utf-8"
request.payload = JSON.stringify request.options.formFields ? null
# strings are utf-16, so string.length can be wrong
request.options.headers["content-length"] = Buffer.byteLength(request.payload, "utf8")
else if not _.isEmpty request.options.formFields
# The `querystring` module copies the key name verbatim, even if the value is actually an array. Things like PHP don't understand this, and expect every array-containing key to be suffixed with []. We'll just append that ourselves, then.
request.options.headers["content-type"] = "application/x-www-form-urlencoded"
request.payload = querystring.stringify formFixArray(request.options.formFields)
# URI encoded string is assumed to be ascii
request.options.headers["content-length"] = request.payload.length
else
request.payload = ""

request.options.headers["content-length"] = request.payload.length
request.options.headers["content-length"] = 0

return Promise.resolve()
else if request.options.formFields? and multipart
Expand Down
6 changes: 4 additions & 2 deletions lib/bhttp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test-utf8-json.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Promise = require "bluebird"
bhttp = require "./"

formatLine = (line) -> line.toString().replace(/\n/g, "\\n").replace(/\r/g, "\\r")

# this just tests we can parse the data we sent. if ever we send
# more data than the content-length it will probably be broken
Promise.try ->
bhttp.post "http://posttestserver.com/post.php",
value: "hello \ud801\udc37",
,
encodeJSON: true,
headers: {"user-agent": "bhttp/test POST UTF-8 JSON"}
.then (response) ->
responseUrl = response.body.toString().split("\n")[1].replace(/^View it at /, "")
console.log responseUrl
bhttp.get responseUrl
.then (response) ->
lines = response.body.toString().split("\n")
JSON.parse lines[lines.length-1]
.then (obj) ->
console.log "POST UTF-8 string", obj