• Web

Parsing Cookies

From the class:  Session Cookies

So far, the cookies on the server are just going to be a string like this, a string of cookie values. And we can get access to them by inspecting the request headers and looking at the cookie header. But this value is just going to be some string that looks like this down here.

So the first job of a cookie session framework is going to be to parse these cookies into an object that we can work with, just like we would any other object in memory. To do that, let's create a function called parseCookie. And I'm going to pass it a request. And what I want this to do is to return, or to assign to the request object, a property called cookies. And I want that to be equal to an object of cookies.

And those cookies will have as keys the cookie names, and the values will be the values for that cookie. So I'll create a function up here called parseCookie. And we'll write that together so you get a sense of what's required. This will be the ultimate test if I can actually code and talk at the same time. I'll do my best.

We're going to start off by creating an empty object. We'll call it cookies, because that's where we're going to put the cookie keys and values. Then I'll create a variable for the pairs of cookies that we're going to parse out. And first we need to check if there's actually any cookie header. If there's no cookie header, we don't need to do anything. So that's going to be under headers.cookie.

And I'll assign to the variable pairs the result of splitting out the different cookies in the string. And so to do that, we can call the split method on the string. And either I can pass it the literal thing that I want to split by, or to be a little bit more specific, I'm going to use a regular expression. So I'm going to split by a semicolon and a space, and then anything after that space. Now, for each pair, or each cookie, I need to parse out the key and the value, so use a little ES6 syntax here.

And I'll grab the parts by splitting the cookie based on the equal sign. So on the left hand side of the split will be the cookie key, and the right hand side will be the values. So this will be element 0 of the array, and this will be element 1 of the array. Let's set the key equal to the key variable, so that'll be part 0. And the value will be part 1.

And then we can set the cookies key equal to the value. So we're populating, now, our empty object that we created up here. And we need to do something a little special here with the value. We can't just take the raw value, and that's because what we really should be doing is encoding the value to be OK for a URL. And what that means is, if you've ever seen a URL where, instead of spaces, you see pluses, and instead of forward slashes, you see something like %F, these are URL encoded values.

And the cookie values should be encoded as well. So what we can do to decode them in Node.js is to use the decodeURIComponent method. If you're using a different runtime or language, you might have a different method for decoding URI components. But this is the same one that you're going to use in the browser. And Node.js calls it the same thing. So decodeURIComponent, and what we'll get back is the value.

And then one last thing we need to do is to assign the cookies property to the request. And now we should be able to access cookies just by looking at the request. So why don't we start off by just logging it out to the console. Again, this is probably not going to win any implementation awards, and we wouldn't actually write it ourselves when we're working on a production application. But I just want to show you the different jobs that the parseCookie function has to do.

Let's go try it out in the console. I'll use cURL to send up a cookie header with two cookies, first and second. And notice that they're delimited again with this semicolon. And when I press Enter, if this all worked correctly, we should be printing out to the console the object. So now we have an object where the key is the cookie key, and the value is whatever value is stored in the cookie for all the cookies.