diff --git a/06_object.md b/06_object.md index 9af33b4e3..3f5f09959 100644 --- a/06_object.md +++ b/06_object.md @@ -664,6 +664,8 @@ console.log(okIterator.next()); {{index "matrix example", "Matrix class", array}} +{{id matrix}} + Let's implement an iterable data structure. We'll build a _matrix_ class, acting as a two-dimensional array. diff --git a/18_http.md b/18_http.md index 670ba1c06..493e22350 100644 --- a/18_http.md +++ b/18_http.md @@ -1,4 +1,4 @@ -{{meta {load_files: ["code/chapter/17_http.js"]}}} +{{meta {load_files: ["code/chapter/18_http.js"]}}} # HTTP and Forms @@ -12,12 +12,12 @@ JavaScript has access to it. {{index "IP address"}} -If you type _eloquentjavascript.net/17_http.html_ into -your browser's ((address bar)), the ((browser)) first looks up the -((address)) of the server associated with _eloquentjavascript.net_ -and tries to open a ((TCP)) ((connection)) to it on ((port)) 80, the -default port for ((HTTP)) traffic. If the ((server)) exists and -accepts the connection, the browser sends something like this: +If you type _eloquentjavascript.net/18_http.html_ into your browser's +((address bar)), the ((browser)) first looks up the ((address)) of the +server associated with _eloquentjavascript.net_ and tries to open a +((TCP)) ((connection)) to it on ((port)) 80, the default port for +((HTTP)) traffic. If the ((server)) exists and accepts the connection, +the browser might send something like this: ```{lang: http} GET /17_http.html HTTP/1.1 @@ -31,19 +31,20 @@ Then the server responds, through that same connection. HTTP/1.1 200 OK Content-Length: 65585 Content-Type: text/html -Last-Modified: Wed, 09 Apr 2014 10:48:09 GMT +Last-Modified: Mon, 08 Jan 2018 10:29:45 GMT ... the rest of the document ``` -The browser then takes the part of the ((response)) after the blank -line and displays it as an ((HTML)) document. +The browser takes the part of the ((response)) after the blank line, +its _body_ (not to be confused with the HTML `
` tag), and +displays it as an ((HTML)) document. {{index HTTP}} -The information sent by the client is called the -_((request))_. It starts with this line: +The information sent by the client is called the _((request))_. It +starts with this line: ```{lang: http} GET /17_http.html HTTP/1.1 @@ -51,35 +52,42 @@ GET /17_http.html HTTP/1.1 {{index "DELETE method", "PUT method", "GET method"}} -The first word is -the _((method))_ of the ((request)). `GET` means that we want to _get_ -the specified resource. Other common methods are `DELETE` to delete a -resource, `PUT` to replace it, and `POST` to send information to it. -Note that the ((server)) is not obliged to carry out every request it -gets. If you walk up to a random website and tell it to `DELETE` its -main page, it'll probably refuse. - -{{index [path, URL], Twitter}} - -The part after the ((method)) name is the path of the -((resource)) the request applies to. In the simplest case, a resource -is simply a ((file)) on the ((server)), but the protocol doesn't -require it to be. A resource may be anything that can be transferred _as if_ -it is a file. Many servers generate the responses they produce on the -fly. For example, if you open -http://twitter.com/marijnjh[_twitter.com/marijnjh_], the server looks -in its database for a user named _marijnjh_, and if it finds one, it +The first word is the _((method))_ of the ((request)). `GET` means +that we want to _get_ the specified resource. Other common methods are +`DELETE` to delete a resource, `PUT` to replace it, and `POST` to send +information to it. Note that the ((server)) is not obliged to carry +out every request it gets. If you walk up to a random website and tell +it to `DELETE` its main page, it'll probably refuse. + +{{index [path, URL], GitHub}} + +The part after the ((method)) name is the path of the ((resource)) the +request applies to. In the simplest case, a resource is simply a +((file)) on the ((server)), but the protocol doesn't require it to be. +A resource may be anything that can be transferred _as if_ it is a +file. Many servers generate the responses they produce on the fly. For +example, if you open +[_github.com/marijnh_](https://github.com/marijnh), the server looks +in its database for a user named "marijnh", and if it finds one, it will generate a profile page for that user. After the resource path, the first line of the request mentions -`HTTP/1.1` to indicate the ((version)) of the ((HTTP)) ((protocol)) -it is using. +`HTTP/1.1` to indicate the ((version)) of the ((HTTP)) ((protocol)) it +is using. + +In practice, many sites use HTTP version 2, which supports the same +concepts as version 1.1, but is, for performance reasons, a lot more +complicated. Browsers will automatically switch to the appropriate +protocol version when talking to a given server, and the outcome of a +request is the same regardless which version is used. Because version +1.1 is more straightforward and easier to play around with, we'll +focus on that. {{index "status code"}} -The server's ((response)) will start with a version -as well, followed by the status of the response, first as a -three-digit status code and then as a human-readable string. +The server's ((response)) will start with a version as well, followed +by the status of the response, first as a three-digit status code and +then as a human-readable string. ```{lang: http} HTTP/1.1 200 OK @@ -90,71 +98,66 @@ HTTP/1.1 200 OK Status codes starting with a 2 indicate that the request succeeded. Codes starting with 4 mean there was something wrong with the ((request)). 404 is probably the most famous HTTP status code—it means -that the resource that was requested could not be found. Codes that -start with 5 mean an error happened on the ((server)) and the request -is not to blame. +that the resource could not be found. Codes that start with 5 mean an +error happened on the ((server)) and the request is not to blame. {{index HTTP}} {{id headers}} -The first line of a request or response may be followed by -any number of _((header))s_. These are lines in the form “name: value” -that specify extra information about the request or response. These -headers were part of the example ((response)): + +The first line of a request or response may be followed by any number +of _((header))s_. These are lines in the form `name: value` that +specify extra information about the request or response. These headers +were part of the example ((response)): ```{lang: null} Content-Length: 65585 Content-Type: text/html -Last-Modified: Wed, 09 Apr 2014 10:48:09 GMT +Last-Modified: Thu, 04 Jan 2018 14:05:30 GMT ``` {{index "Content-Length header", "Content-Type header", "Last-Modified header"}} -This tells us the size and type of the response document. In -this case, it is an HTML document of 65,585 bytes. It also tells us when +This tells us the size and type of the response document. In this +case, it is an HTML document of 65,585 bytes. It also tells us when that document was last modified. {{index "Host header", domain}} -For the most part, a client or server -decides which ((header))s to include in a ((request)) or ((response)), -though a few headers are required. For example, the `Host` header, -which specifies the hostname, should be included in a request -because a ((server)) might be serving multiple hostnames on a single -((IP address)), and without that header, the server won't know which host the -client is trying to talk to. +For the most part, a client or server decides which ((header))s to +include in a ((request)) or ((response)), though a few headers are +required. For example, the `Host` header, which specifies the +hostname, should be included in a request because a ((server)) might +be serving multiple hostnames on a single ((IP address)), and without +that header, the server won't know which host the client is trying to +talk to. {{index "GET method", "DELETE method", "PUT method", "POST method", "body (HTTP)"}} -After the headers, both requests and -responses may include a blank line followed by a _body_, which -contains the data being sent. `GET` and `DELETE` requests don't send -along any data, but `PUT` and `POST` requests do. -Similarly, some response types, such as error responses, do not -require a body. +After the headers, both requests and responses may include a blank +line followed by the body, which contains the data being sent. `GET` +and `DELETE` requests don't send along any data, but `PUT` and `POST` +requests do. Similarly, some response types, such as error responses, +do not require a body. ## Browsers and HTTP {{index HTTP}} -As we saw in the example, a ((browser)) will make a request -when we enter a ((URL)) in its ((address bar)). When the resulting -HTML page references other files, such as ((image))s and JavaScript -((file))s, those are also fetched. +As we saw in the example, a ((browser)) will make a request when we +enter a ((URL)) in its ((address bar)). When the resulting HTML page +references other files, such as ((image))s and JavaScript ((file))s, +those are also retrieved. {{index parallelism, "GET method"}} -A moderately complicated ((website)) can easily -include anywhere from 10 to 200 ((resource))s. To be able to -fetch those quickly, browsers will make several requests -simultaneously, rather than waiting for the responses one at a time. -Such documents are always fetched using `GET` -((request))s. +A moderately complicated ((website)) can easily include anywhere from +10 to 200 ((resource))s. To be able to fetch those quickly, browsers +will make several `GET` requests simultaneously, rather than waiting +for the responses one at a time. -{{id http_forms}} -HTML pages may include _((form))s_, which allow -the user to fill out information and send it to the server. This is an -example of a form: +HTML pages may include _((form))s_, which allow the user to fill out +information and send it to the server. This is an example of a form: ```{lang: "text/html"}