3

I need to authenticate against a proxy server, and after the first HTTP request the I need to parse the proxy-authenticate header string to get the relevant values.

The response headers look something like this,

{ 'content-type': 'text/plain',
  'proxy-authenticate': 'Digest realm="zippy", qop="auth",nonce="c1e1c76b5df5a8cdc921b48d6a7b5493", algorithm="MD5", stale="false"',
   date: 'Thu, 21 Apr 2016 00:19:28 GMT',
   connection: 'close',
  'transfer-encoding': 'chunked' }

I want to extract the proxy-authenticate parameters (i.e. realm, qop, etc.), from the string.

It seems like there must be some super simple way to do this, but I'm just not finding it.

2
  • Is this an AJAX request? Please see this question and this question. Commented Apr 21, 2016 at 0:36
  • Hi Kevin, no this is not AJAX. it is just a straight post from Node using the http library. Commented Apr 21, 2016 at 14:00

1 Answer 1

2

Simply extract your key from the JSON, and split the value by ,. Then again split each value of resulting array using =.

$(document).ready(function() {
  var data = {
    'content-type': 'text/plain',
    'proxy-authenticate': 'Digest realm="zippy", qop="auth",nonce="c1e1c76b5df5a8cdc921b48d6a7b5493", algorithm="MD5", stale="false"',
    date: 'Thu, 21 Apr 2016 00:19:28 GMT',
    connection: 'close',
    'transfer-encoding': 'chunked'
  };

  var header = data['proxy-authenticate'];
  var contents = header.split(',');

  var pairs = {};
  $.each(contents, function(index, value) {

    var pair = value.split('=');
    pairs[pair[0]] = pair[1];
  });

  $('div').html(JSON.stringify(pairs));
});

Here is a demo.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately fails if one of the values contains a comma or equal signe. E.g. realm="x,y" or nonce="abcdef==". And it further lists " qop" with leading space!
@Joe The input must be cleaned before trying to extract data from it. E.g. If your input can guarantee that each value is of format {<key1>="<value1>",<key2>="<value2>"} then instead of splitting on , you can split on ", which will solve your comma inside a value problem. Of course if the value itself contains ", then program will again fail. Any data before parsing needs to guarantee a format and based on the format the parser can be written. Let me know your thoughts.
@Joe The second problem of properties having multiple = cannot be solved easily. Imagine the property abc=d=ef=g. In this case no one knows which = is the delimeter, and hence this value can never be parsed correctly on its own. As I said, the input you are getting needs to follow a pattern or define a contract. Without that parsing would be hard. Let me know your thoughts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.