Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate ch09-00 in french. #18

Open
wants to merge 3 commits into
base: french-release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions FRENCH/examples-sources/09_01_sync_tcp_server/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<h1>Oops!</h1>
<p>Sorry, I don't know what you're asking for.</p>
</body>
</html>
9 changes: 9 additions & 0 deletions FRENCH/examples-sources/09_01_sync_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "sync_tcp_server"
version = "0.1.0"
authors = ["Your Name <[email protected]"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
11 changes: 11 additions & 0 deletions FRENCH/examples-sources/09_01_sync_tcp_server/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Hi from Rust</p>
</body>
</html>
39 changes: 39 additions & 0 deletions FRENCH/examples-sources/09_01_sync_tcp_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::fs;
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;

fn main() {
// Listen for incoming TCP connections on localhost port 7878
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();

// Block forever, handling each request that arrives at this IP address
for stream in listener.incoming() {
let stream = stream.unwrap();

handle_connection(stream);
}
}

fn handle_connection(mut stream: TcpStream) {
// Read the first 1024 bytes of data from the stream
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();

let get = b"GET / HTTP/1.1\r\n";

// Respond with greetings or a 404,
// depending on the data in the request
let (status_line, filename) = if buffer.starts_with(get) {
("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
} else {
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
};
let contents = fs::read_to_string(filename).unwrap();

// Write response back to the stream,
// and flush the stream to ensure the response is sent back to the client
let response = format!("{status_line}{contents}");
stream.write_all(response.as_bytes()).unwrap();
stream.flush().unwrap();
}
1 change: 1 addition & 0 deletions FRENCH/examples-sources/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[workspace]
members = [
"09_01_sync_tcp_server",
]
11 changes: 11 additions & 0 deletions FRENCH/examples/09_01_sync_tcp_server/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Salutations !</title>
</head>
<body>
<h1>Oups !</h1>
<p>Désolé, je ne connaît pas ce que vous demandez.</p>
</body>
</html>
9 changes: 9 additions & 0 deletions FRENCH/examples/09_01_sync_tcp_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "sync_tcp_server"
version = "0.1.0"
authors = ["Your Name <[email protected]"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
11 changes: 11 additions & 0 deletions FRENCH/examples/09_01_sync_tcp_server/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Salutations !</title>
</head>
<body>
<h1>Salut !</h1>
<p>Bonjour de la part de Rust</p>
</body>
</html>
40 changes: 40 additions & 0 deletions FRENCH/examples/09_01_sync_tcp_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fs;
use std::io::prelude::*;
use std::net::TcpListener;
use std::net::TcpStream;

fn main() {
// Ecoute les connexions TCP entrantes sur localhost, port 7878.
let ecouteur = TcpListener::bind("127.0.0.1:7878").unwrap();

// Bloque pour toujours, gérant chaque requête qui arrive
// sur cette adresse IP.
for flux in ecouteur.incoming() {
let flux = flux.unwrap();

gestion_connexion(flux);
}
}

fn gestion_connexion(mut flux: TcpStream) {
// Lit les 1024 premiers octets de données présents dans le flux
let mut tampon = [0; 1024];
flux.read(&mut tampon).unwrap();

let get = b"GET / HTTP/1.1\r\n";

// Répond avec l'accueil ou une erreur 404,
// en fonction des données présentes dans la requête
let (ligne_statut, nom_fichier) = if tampon.starts_with(get) {
("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
} else {
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
};
let contenu = fs::read_to_string(nom_fichier).unwrap();

// Ecrit la réponse dans le flux, et purge le flux pour s'assurer
// que la réponse est bien renvoyée au client
let reponse = format!("{ligne_statut}{contenu}");
flux.write_all(reponse.as_bytes()).unwrap();
flux.flush().unwrap();
}
1 change: 1 addition & 0 deletions FRENCH/examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[workspace]
members = [
"09_01_sync_tcp_server",
]
66 changes: 66 additions & 0 deletions FRENCH/src/09_example/00_intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!--
# Final Project: Building a Concurrent Web Server with Async Rust
In this chapter, we'll use asynchronous Rust to modify the Rust book's
[single-threaded web server](https://doc.rust-lang.org/book/ch20-01-single-threaded.html)
to serve requests concurrently.
## Recap
Here's what the code looked like at the end of the lesson.
-->

# Projet final : construire un serveur web concurrent avec le Rust asynchrone

Dans ce chapitre, nous allons utiliser le Rust asynchrone pour modifier le
[serveur web mono-processus](https://jimskapt.github.io/rust-book-fr/ch20-00-final-project-a-web-server.html)
du livre sur Rust, afin qu'il serve les requêtes de manière concurrente.

## Résumé

Voici ce à quoi ressemblera le code à la fin de cette leçon.

<!--
`src/main.rs`:
```rust
{{#include ../../examples-sources/09_01_sync_tcp_server/src/main.rs}}
```
-->

`src/main.rs` :

```rust
{{#include ../../examples/09_01_sync_tcp_server/src/main.rs}}
```

<!--
`hello.html`:
```html
{{#include ../../examples-sources/09_01_sync_tcp_server/hello.html}}
```
-->

`hello.html` :

```html
{{#include ../../examples/09_01_sync_tcp_server/hello.html}}
```

<!--
`404.html`:
```html
{{#include ../../examples-sources/09_01_sync_tcp_server/404.html}}
```
-->

`404.html` :

```html
{{#include ../../examples/09_01_sync_tcp_server/404.html}}
```

<!--
If you run the server with `cargo run` and visit `127.0.0.1:7878` in your browser,
you'll be greeted with a friendly message from Ferris!
-->

Si vous exécutez le serveur avec `cargo run` et visitez `127.0.0.1:7878` dans
votre navigateur, vous allez être accueilli par un message chaleureux de
Ferris !
2 changes: 2 additions & 0 deletions FRENCH/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Table des matières

- [Projet final : Serveur HTTP](09_example/00_intro.md)

[Traduction des termes](translation-terms.md)