@@ -34,14 +34,13 @@ Now enter the code in Listing 21-1 in *src/main.rs* to start. This code will
34
34
listen at the local address ` 127.0.0.1:7878 ` for incoming TCP streams. When it
35
35
gets an incoming stream, it will print ` Connection established! ` .
36
36
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 " >
38
38
39
39
``` rust,no_run
40
40
{{#rustdoc_include ../listings/ch21-web-server/listing-21-01/src/main.rs}}
41
41
```
42
42
43
- <span class =" caption " >Listing 21-1: Listening for incoming streams and printing
44
- a message when we receive a stream</span >
43
+ </Listing >
45
44
46
45
Using ` TcpListener ` , we can listen for TCP connections at the address
47
46
` 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
127
126
print it so we can see the data being sent from the browser. Change the code to
128
127
look like Listing 21-2.
129
128
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 " >
131
130
132
131
``` rust,no_run
133
132
{{#rustdoc_include ../listings/ch21-web-server/listing-21-02/src/main.rs}}
134
133
```
135
134
136
- <span class =" caption " >Listing 21-2: Reading from the ` TcpStream ` and printing
137
- the data</span >
135
+ </Listing >
138
136
139
137
We bring ` std::io::prelude ` and ` std::io::BufReader ` into scope to get access
140
138
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
273
271
` println! ` that was printing the request data and replace it with the code in
274
272
Listing 21-3.
275
273
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 " >
277
275
278
276
``` rust,no_run
279
277
{{#rustdoc_include ../listings/ch21-web-server/listing-21-03/src/main.rs:here}}
280
278
```
281
279
282
- <span class =" caption " >Listing 21-3: Writing a tiny successful HTTP response to
283
- the stream</span >
280
+ </Listing >
284
281
285
282
The first new line defines the ` response ` variable that holds the success
286
283
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
302
299
* src* directory. You can input any HTML you want; Listing 21-4 shows one
303
300
possibility.
304
301
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 " >
306
303
307
304
``` html
308
305
{{#include ../listings/ch21-web-server/listing-21-05/hello.html}}
309
306
```
310
307
311
- <span class =" caption " >Listing 21-4: A sample HTML file to return in a
312
- response</span >
308
+ </Listing >
313
309
314
310
This is a minimal HTML5 document with a heading and some text. To return this
315
311
from the server when a request is received, we’ll modify ` handle_connection ` as
316
312
shown in Listing 21-5 to read the HTML file, add it to the response as a body,
317
313
and send it.
318
314
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 " >
320
316
321
317
``` rust,no_run
322
318
{{#rustdoc_include ../listings/ch21-web-server/listing-21-05/src/main.rs:here}}
323
319
```
324
320
325
- <span class =" caption " >Listing 21-5: Sending the contents of * hello.html* as the
326
- body of the response</span >
321
+ </Listing >
327
322
328
323
We’ve added ` fs ` to the ` use ` statement to bring the standard library’s
329
324
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
356
351
received against what we know a request for * /* looks like and adds ` if ` and
357
352
` else ` blocks to treat requests differently.
358
353
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 " >
360
355
361
356
``` rust,no_run
362
357
{{#rustdoc_include ../listings/ch21-web-server/listing-21-06/src/main.rs:here}}
363
358
```
364
359
365
- <span class =" caption " >Listing 21-6: Handling requests to * /* differently from
366
- other requests</span >
360
+ </Listing >
367
361
368
362
We’re only going to be looking at the first line of the HTTP request, so rather
369
363
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
390
384
not found. We’ll also return some HTML for a page to render in the browser
391
385
indicating the response to the end user.
392
386
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 " >
394
388
395
389
``` rust,no_run
396
390
{{#rustdoc_include ../listings/ch21-web-server/listing-21-07/src/main.rs:here}}
397
391
```
398
392
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 >
401
394
402
395
Here, our response has a status line with status code 404 and the reason phrase
403
396
` NOT FOUND ` . The body of the response will be the HTML in the file * 404.html* .
404
397
You’ll need to create a * 404.html* file next to * hello.html* for the error
405
398
page; again feel free to use any HTML you want or use the example HTML in
406
399
Listing 21-8.
407
400
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 " >
409
402
410
403
``` html
411
404
{{#include ../listings/ch21-web-server/listing-21-07/404.html}}
412
405
```
413
406
414
- <span class =" caption " >Listing 21-8: Sample content for the page to send back
415
- with any 404 response</span >
407
+ </Listing >
416
408
417
409
With these changes, run your server again. Requesting * 127.0.0.1:7878* should
418
410
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
429
421
and write the response. Listing 21-9 shows the resulting code after replacing
430
422
the large ` if ` and ` else ` blocks.
431
423
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 " >
433
425
434
426
``` rust,no_run
435
427
{{#rustdoc_include ../listings/ch21-web-server/listing-21-09/src/main.rs:here}}
436
428
```
437
429
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 >
440
431
441
432
Now the ` if ` and ` else ` blocks only return the appropriate values for the
442
433
status line and filename in a tuple; we then use destructuring to assign these
0 commit comments