javascript - How to parse the first line of the http header? - Stack Overflow

Is there any javascript function, to parse the first line of the http header?GET page?id=173&sess

Is there any javascript function, to parse the first line of the http header?

GET /page/?id=173&sessid=mk9sa774 HTTP/1.1

The url is encoded.

I would like to get an object, like this:

{
"method" : "GET",
"url" : "/page/",
"parameters": {
  "id" : 173,
  "sessid" : "mk9sa774"
  }
}

I searched a lot, but I haven't found anything useful.

thanks in advance,

Is there any javascript function, to parse the first line of the http header?

GET /page/?id=173&sessid=mk9sa774 HTTP/1.1

The url is encoded.

I would like to get an object, like this:

{
"method" : "GET",
"url" : "/page/",
"parameters": {
  "id" : 173,
  "sessid" : "mk9sa774"
  }
}

I searched a lot, but I haven't found anything useful.

thanks in advance,

Share Improve this question edited Mar 25, 2012 at 21:11 James M 16.7k3 gold badges49 silver badges57 bronze badges asked Mar 25, 2012 at 21:10 Danny FoxDanny Fox 40.8k29 gold badges71 silver badges96 bronze badges 2
  • In what environment? The browser? – James M Commented Mar 25, 2012 at 21:12
  • 1 have you got the line as a javascript string? – boisvert Commented Mar 25, 2012 at 21:18
Add a ment  | 

1 Answer 1

Reset to default 7

First you can split on spaces:

var lineParts = line.split(' ');

Now you can get the method, unparsed path, and version:

var method  = lineParts[0];
var path    = lineParts[1];
var version = lineParts[2];

Then you can split up the path into the query string and non-query string parts:

var queryStringIndex = path.indexOf('?');
var url, queryString;
if(queryStringIndex == -1) {
    url = path, queryString = '';
}else{
    url = path.substring(0, queryStringIndex);
    // I believe that technically the query string includes the '?',
    // but that's not important for us.
    queryString = path.substring(queryStringIndex + 1);
}

If there is a query string, we can then split it up into key=value strings:

var queryStringParts = [];
if(queryStringIndex != -1) {
    queryStringParts = queryString.split('&');
}

Then we can unescape them and stuff them into an object:

var parameters = {};
queryStringParts.forEach(function(part) {
    var equalsIndex = part.indexOf('=');
    var key, value;
    if(equalsIndex == -1) {
        key = part, value = "";
    }else{
        key   = part.substring(0, equalsIndex);
        value = part.substring(equalsIndex + 1);
    }
    key   = decodeURIComponent(key);
    value = decodeURIComponent(value);
    parameters[key] = value;
});

If you really wanted to, you could then put all that data into an object:

return {
    method:     method,
    url:        url,
    version:    version,
    parameters: parameters
};

If you're in a browser environment, that's the only way to do it. If you're using Node.JS, it can deal with the URL parsing for you.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744772047a4592816.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信