Skip to content

Commit ea934e6

Browse files
committed
Adopt <Listing> for Ch. 21
1 parent ca31f72 commit ea934e6

File tree

3 files changed

+77
-82
lines changed

3 files changed

+77
-82
lines changed

src/ch21-01-single-threaded.md

+18-27
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ Now enter the code in Listing 21-1 in *src/main.rs* to start. This code will
3434
listen at the local address `127.0.0.1:7878` for incoming TCP streams. When it
3535
gets an incoming stream, it will print `Connection established!`.
3636

37-
<span class="filename">Filename: src/main.rs</span>
37+
<Listing number="21-1" file-name="src/main.rs" caption="Listening for incoming streams and printing a message when we receive a stream">
3838

3939
```rust,no_run
4040
{{#rustdoc_include ../listings/ch21-web-server/listing-21-01/src/main.rs}}
4141
```
4242

43-
<span class="caption">Listing 21-1: Listening for incoming streams and printing
44-
a message when we receive a stream</span>
43+
</Listing>
4544

4645
Using `TcpListener`, we can listen for TCP connections at the address
4746
`127.0.0.1:7878`. In the address, the section before the colon is an IP address
@@ -127,14 +126,13 @@ this new `handle_connection` function, we’ll read data from the TCP stream and
127126
print it so we can see the data being sent from the browser. Change the code to
128127
look like Listing 21-2.
129128

130-
<span class="filename">Filename: src/main.rs</span>
129+
<Listing number="21-2" file-name="src/main.rs" caption="Reading from the `TcpStream` and printing the data">
131130

132131
```rust,no_run
133132
{{#rustdoc_include ../listings/ch21-web-server/listing-21-02/src/main.rs}}
134133
```
135134

136-
<span class="caption">Listing 21-2: Reading from the `TcpStream` and printing
137-
the data</span>
135+
</Listing>
138136

139137
We bring `std::io::prelude` and `std::io::BufReader` into scope to get access
140138
to traits and types that let us read from and write to the stream. In the `for`
@@ -273,14 +271,13 @@ successful request! From the `handle_connection` function, remove the
273271
`println!` that was printing the request data and replace it with the code in
274272
Listing 21-3.
275273

276-
<span class="filename">Filename: src/main.rs</span>
274+
<Listing number="21-3" file-name="src/main.rs" caption="Writing a tiny successful HTTP response to the stream">
277275

278276
```rust,no_run
279277
{{#rustdoc_include ../listings/ch21-web-server/listing-21-03/src/main.rs:here}}
280278
```
281279

282-
<span class="caption">Listing 21-3: Writing a tiny successful HTTP response to
283-
the stream</span>
280+
</Listing>
284281

285282
The first new line defines the `response` variable that holds the success
286283
message’s data. Then we call `as_bytes` on our `response` to convert the string
@@ -302,28 +299,26 @@ the new file *hello.html* in the root of your project directory, not in the
302299
*src* directory. You can input any HTML you want; Listing 21-4 shows one
303300
possibility.
304301

305-
<span class="filename">Filename: hello.html</span>
302+
<Listing number="21-4" file-name="hello.html" caption="A sample HTML file to return in a response">
306303

307304
```html
308305
{{#include ../listings/ch21-web-server/listing-21-05/hello.html}}
309306
```
310307

311-
<span class="caption">Listing 21-4: A sample HTML file to return in a
312-
response</span>
308+
</Listing>
313309

314310
This is a minimal HTML5 document with a heading and some text. To return this
315311
from the server when a request is received, we’ll modify `handle_connection` as
316312
shown in Listing 21-5 to read the HTML file, add it to the response as a body,
317313
and send it.
318314

319-
<span class="filename">Filename: src/main.rs</span>
315+
<Listing number="21-5" file-name="src/main.rs" caption="Sending the contents of *hello.html* as the body of the response">
320316

321317
```rust,no_run
322318
{{#rustdoc_include ../listings/ch21-web-server/listing-21-05/src/main.rs:here}}
323319
```
324320

325-
<span class="caption">Listing 21-5: Sending the contents of *hello.html* as the
326-
body of the response</span>
321+
</Listing>
327322

328323
We’ve added `fs` to the `use` statement to bring the standard library’s
329324
filesystem module into scope. The code for reading the contents of a file to a
@@ -356,14 +351,13 @@ as shown in Listing 21-6. This new code checks the content of the request
356351
received against what we know a request for */* looks like and adds `if` and
357352
`else` blocks to treat requests differently.
358353

359-
<span class="filename">Filename: src/main.rs</span>
354+
<Listing number="21-6" file-name="src/main.rs" caption="Handling requests to */* differently from other requests">
360355

361356
```rust,no_run
362357
{{#rustdoc_include ../listings/ch21-web-server/listing-21-06/src/main.rs:here}}
363358
```
364359

365-
<span class="caption">Listing 21-6: Handling requests to */* differently from
366-
other requests</span>
360+
</Listing>
367361

368362
We’re only going to be looking at the first line of the HTTP request, so rather
369363
than reading the entire request into a vector, we’re calling `next` to get the
@@ -390,29 +384,27 @@ with the status code 404, which signals that the content for the request was
390384
not found. We’ll also return some HTML for a page to render in the browser
391385
indicating the response to the end user.
392386

393-
<span class="filename">Filename: src/main.rs</span>
387+
<Listing number="21-7" file-name="src/main.rs" caption="Responding with status code 404 and an error page if anything other than */* was requested">
394388

395389
```rust,no_run
396390
{{#rustdoc_include ../listings/ch21-web-server/listing-21-07/src/main.rs:here}}
397391
```
398392

399-
<span class="caption">Listing 21-7: Responding with status code 404 and an
400-
error page if anything other than */* was requested</span>
393+
</Listing>
401394

402395
Here, our response has a status line with status code 404 and the reason phrase
403396
`NOT FOUND`. The body of the response will be the HTML in the file *404.html*.
404397
You’ll need to create a *404.html* file next to *hello.html* for the error
405398
page; again feel free to use any HTML you want or use the example HTML in
406399
Listing 21-8.
407400

408-
<span class="filename">Filename: 404.html</span>
401+
<Listing number="21-8" file-name="404.html" caption="Sample content for the page to send back with any 404 response">
409402

410403
```html
411404
{{#include ../listings/ch21-web-server/listing-21-07/404.html}}
412405
```
413406

414-
<span class="caption">Listing 21-8: Sample content for the page to send back
415-
with any 404 response</span>
407+
</Listing>
416408

417409
With these changes, run your server again. Requesting *127.0.0.1:7878* should
418410
return the contents of *hello.html*, and any other request, like
@@ -429,14 +421,13 @@ we can then use those variables unconditionally in the code to read the file
429421
and write the response. Listing 21-9 shows the resulting code after replacing
430422
the large `if` and `else` blocks.
431423

432-
<span class="filename">Filename: src/main.rs</span>
424+
<Listing number="21-9" file-name="src/main.rs" captionn="Refactoring the `if` and `else` blocks to contain only the code that differs between the two cases">
433425

434426
```rust,no_run
435427
{{#rustdoc_include ../listings/ch21-web-server/listing-21-09/src/main.rs:here}}
436428
```
437429

438-
<span class="caption">Listing 21-9: Refactoring the `if` and `else` blocks to
439-
contain only the code that differs between the two cases</span>
430+
</Listing>
440431

441432
Now the `if` and `else` blocks only return the appropriate values for the
442433
status line and filename in a tuple; we then use destructuring to assign these

0 commit comments

Comments
 (0)