Skip to content

Commit

Permalink
allow headers to override
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Treacy committed May 21, 2011
1 parent db7a786 commit 9e00257
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/index.html
Expand Up @@ -174,6 +174,12 @@ <h3>Meta</h3>
<h4 id="uri-encoded">URI-encoded bucket/keys</h4>

<p>An option (<code>Meta</code> property) <code>encodeUri</code> can be set to <code>true</code> when you want to have your bucket and key URI-encoded. The default is <code>false</code> because <code>Meta</code> is implementation-agnostic, and this only makes sense for HTTP clients.</p>

<h4 id="headers">HTTP headers</h4>

<p>You can pass in a <code>headers</code> options that will override any previously set header. Useful to set other headers if you have a reverse proxy sitting between node and Riak. <em>Use with caution</em>.</p>

<pre><code>db.get('flights', 'KLM-5034', { headers: { Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==' } })</code></pre>

<h4>Content detection</h4>

Expand Down
8 changes: 7 additions & 1 deletion spec/test_http_meta.coffee
Expand Up @@ -43,18 +43,24 @@ vows.describe('Meta for HTTP').addBatch(
assert.equal meta.contentType, 'text/rtf'
assert.equal meta.path, '/riak/bucket/key'

'a meta with some properties':
'a meta with some properties and headers':
topic: ->
meta = new Meta 'bucket', 'key', {
links: [{ bucket: 'test', key: 'doc%2$@', tag: 'next' }]
fire: true
overridable: true
headers: { Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', 'X-Riak-Meta-overridable': 'yes!' }
}
meta.toHeaders()

'parses them correctly': (headers) ->
assert.notEqual headers.statusCode?
assert.equal headers['X-Riak-Meta-fire'], 'true'
assert.equal headers['Link'], '</riak/test/doc%252%24%40>; riaktag="next"'

'overrides them correctly': (headers) ->
assert.equal headers['X-Riak-Meta-overridable'], 'yes!'
assert.equal headers['Authorization'], 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='

'a meta partly loaded with POST response headers':
topic: ->
Expand Down
4 changes: 2 additions & 2 deletions src/http_client.coffee
Expand Up @@ -198,9 +198,9 @@ class HttpClient extends Client
# private

execute: (verb, meta, callback) ->

meta.method = verb.toUpperCase()
meta.headers = meta.toHeaders()
meta.headers = meta.toHeaders()
Client.log "#{meta.method} #{meta.path}", meta

request = http.request meta, (response) =>
Expand Down
7 changes: 6 additions & 1 deletion src/http_meta.coffee
Expand Up @@ -65,6 +65,7 @@ class Meta extends CoreMeta
# ignored info: binary, raw, url, path

toHeaders: ->

headers = {}

# remove client id if there's no vclock
Expand All @@ -89,7 +90,11 @@ class Meta extends CoreMeta
# don't send chunked data at least until riak #278 gets fixed or we can stream the req body
headers['Content-Length'] =
if @data instanceof Buffer then @data.length else Buffer.byteLength(@data)


if @headers
for k of @headers then headers[k] = @headers[k]
delete @headers

return headers

doEncodeUri: (component = '') ->
Expand Down
10 changes: 4 additions & 6 deletions src/meta.coffee
Expand Up @@ -8,7 +8,7 @@ class Meta
if arguments.length is 1 and bucket instanceof Object
options = bucket # (in case the first arg is an object)
[bucket, key] = [options.bucket, options.key]

@load options
@bucket = bucket
@key = key
Expand Down Expand Up @@ -93,7 +93,7 @@ class Meta
options.links = [options.links] if options?.links and not Array.isArray(options.links)

@usermeta = options?.usermeta or {} # get previous usermeta
@usermeta = Utils.mixin true, @usermeta, defaults, this, options
@usermeta = Utils.mixin true, @usermeta, defaults, this, options
delete @usermeta.usermeta # remove old, mixed-in usermeta

props = Utils.uniq Meta.riakProperties
Expand All @@ -106,10 +106,7 @@ class Meta
this[key] = value
else
delete this[key]

# remove useless props that are not popped out and remain in usermeta
delete @usermeta.headers
delete @usermeta.agent


# Pull the value at the given key from the given object, and then removes
# it from the object.
Expand Down Expand Up @@ -163,6 +160,7 @@ Meta.riakProperties = [
'range'
'contentRange'
'acceptRanges'
'headers' # http, we want to keep these so that users can override headers
]

# Defaults for Meta properties
Expand Down

0 comments on commit 9e00257

Please sign in to comment.