src.myserver.http

A package for learning network programming in Python.

This module (file) provides information relative to the HTTP specification.

  1######################################################################
  2# Copyright (c) Adrien Luxey-Bitri, Boris Baldassari
  3#
  4# This program and the accompanying materials are made
  5# available under the terms of the Eclipse Public License 2.0
  6# which is available at https://www.eclipse.org/legal/epl-2.0/
  7#
  8# SPDX-License-Identifier: EPL-2.0
  9######################################################################
 10
 11"""
 12A package for learning network programming in Python.
 13
 14This module (file) provides information relative to the HTTP specification.
 15"""
 16
 17
 18def get_http_code(code: int):
 19    """Returns a dict corresponding to the HTTP status code.
 20
 21    See also : https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
 22    
 23    Parameters
 24    ----------
 25    - code: int 
 26        An HTTP code.
 27
 28    Returns
 29    -------
 30    dict 
 31        Information about the HTTP code, containing fileds:
 32        - header: str 
 33            The code string to put in an HTTP reply header.
 34        - html: str 
 35            The HTML to reply as HTTP content.
 36    """
 37
 38    if code == 200:
 39        return {
 40            "header": "200 OK",
 41            "html": ""
 42        }
 43    elif code == 403:
 44        return {
 45            "header": "403 Forbidden",
 46            "html": """<html>
 47<body>
 48    <h1>Erreur 403 : Interdit</h1>
 49    <p>Une porte fermée se tient devant vous ; et vous n'avez pas la clé.</p>
 50</body>
 51</html>
 52"""
 53        }
 54    elif code == 404:
 55        return {
 56            "header": "404 Not Found",
 57            "html": """<html>
 58<body>
 59    <h1>Erreur 404</h1>
 60    <p>Vous avez traversé les limites du Web. Où que vous soyez, ce n'est sur aucune carte.</p>
 61</body>
 62</html>
 63"""
 64        }
 65    elif code == 501:
 66        return {
 67            "header": "501 Not implemented",
 68            "html": """<html>
 69<body>
 70    <h1>Erreur 501 : Non implémenté</h1>
 71    <p>Ce que vous demandez est acceptable, mais on ne fait pas ça chez nous.</p>
 72</body>
 73</html>
 74"""
 75        }
 76    else: # 500
 77        return {
 78            "header": "500 Internal Server Error",
 79            "html": """<html>
 80<body>
 81    <h1>Erreur 500 : InTERNal SRveR ER0ooOR</h1>
 82    <p>Erreur serveur inconnue.</p>
 83</body>
 84</html>
 85"""
 86        }
 87
 88# From: https://source.chromium.org/chromium/chromium/src/+/main:net/base/mime_util.cc;l=147
 89# The Chromium authors, 2012, BSD Licence
 90file_extension_to_content_type = {
 91    "webm": "video/webm",
 92    "mp3": "audio/mpeg",
 93    "wasm": "application/wasm",
 94    "crx": "application/x-chrome-extension",
 95    "xhtml": "application/xhtml+xml",
 96    "xht": "application/xhtml+xml",
 97    "xhtm": "application/xhtml+xml",
 98    "flac": "audio/flac",
 99    "ogg": "audio/ogg",
100    "oga": "audio/ogg",
101    "opus": "audio/ogg",
102    "wav": "audio/wav",
103    "m4a": "audio/x-m4a",
104    "avif": "image/avif",
105    "gif": "image/gif",
106    "jpeg": "image/jpeg",
107    "jpg": "image/jpeg",
108    "png": "image/png",
109    "apng": "image/apng",
110    "svg": "image/svg+xml",
111    "svgz": "image/svg+xml",
112    "webp": "image/webp",
113    "mht": "multipart/related",
114    "mhtml": "multipart/related",
115    "css": "text/css",
116    "html": "text/html",
117    "htm": "text/html",
118    "shtml": "text/html",
119    "shtm": "text/html",
120    "js": "text/javascript",
121    "mjs": "text/javascript",
122    "xml": "text/xml",
123    "mp4": "video/mp4",
124    "m4v": "video/mp4",
125    "ogv": "video/ogg",
126    "ogm": "video/ogg",
127    "csv": "text/csv",
128    "ico": "image/vnd.microsoft.icon"
129}
130
131def get_http_content_type(extension: str):
132    """Returns the HTTP Content-Type corresponding to a file extension.
133
134    Returns "application/octet-stream" when the extension is unknown.
135
136    Parameters
137    ----------
138    - extension: str
139        A file extension.
140
141    Returns
142    -------
143    str 
144        An HTTP Content-Type 
145    """
146
147    if file_extension_to_content_type.get(extension) is None:
148        return "application/octet-stream"
149    return file_extension_to_content_type[extension]
def get_http_code(code: int):
19def get_http_code(code: int):
20    """Returns a dict corresponding to the HTTP status code.
21
22    See also : https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
23    
24    Parameters
25    ----------
26    - code: int 
27        An HTTP code.
28
29    Returns
30    -------
31    dict 
32        Information about the HTTP code, containing fileds:
33        - header: str 
34            The code string to put in an HTTP reply header.
35        - html: str 
36            The HTML to reply as HTTP content.
37    """
38
39    if code == 200:
40        return {
41            "header": "200 OK",
42            "html": ""
43        }
44    elif code == 403:
45        return {
46            "header": "403 Forbidden",
47            "html": """<html>
48<body>
49    <h1>Erreur 403 : Interdit</h1>
50    <p>Une porte fermée se tient devant vous ; et vous n'avez pas la clé.</p>
51</body>
52</html>
53"""
54        }
55    elif code == 404:
56        return {
57            "header": "404 Not Found",
58            "html": """<html>
59<body>
60    <h1>Erreur 404</h1>
61    <p>Vous avez traversé les limites du Web. Où que vous soyez, ce n'est sur aucune carte.</p>
62</body>
63</html>
64"""
65        }
66    elif code == 501:
67        return {
68            "header": "501 Not implemented",
69            "html": """<html>
70<body>
71    <h1>Erreur 501 : Non implémenté</h1>
72    <p>Ce que vous demandez est acceptable, mais on ne fait pas ça chez nous.</p>
73</body>
74</html>
75"""
76        }
77    else: # 500
78        return {
79            "header": "500 Internal Server Error",
80            "html": """<html>
81<body>
82    <h1>Erreur 500 : InTERNal SRveR ER0ooOR</h1>
83    <p>Erreur serveur inconnue.</p>
84</body>
85</html>
86"""
87        }

Returns a dict corresponding to the HTTP status code.

See also : https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

Parameters

  • code: int An HTTP code.

Returns

dict Information about the HTTP code, containing fileds: - header: str The code string to put in an HTTP reply header. - html: str The HTML to reply as HTTP content.

file_extension_to_content_type = {'webm': 'video/webm', 'mp3': 'audio/mpeg', 'wasm': 'application/wasm', 'crx': 'application/x-chrome-extension', 'xhtml': 'application/xhtml+xml', 'xht': 'application/xhtml+xml', 'xhtm': 'application/xhtml+xml', 'flac': 'audio/flac', 'ogg': 'audio/ogg', 'oga': 'audio/ogg', 'opus': 'audio/ogg', 'wav': 'audio/wav', 'm4a': 'audio/x-m4a', 'avif': 'image/avif', 'gif': 'image/gif', 'jpeg': 'image/jpeg', 'jpg': 'image/jpeg', 'png': 'image/png', 'apng': 'image/apng', 'svg': 'image/svg+xml', 'svgz': 'image/svg+xml', 'webp': 'image/webp', 'mht': 'multipart/related', 'mhtml': 'multipart/related', 'css': 'text/css', 'html': 'text/html', 'htm': 'text/html', 'shtml': 'text/html', 'shtm': 'text/html', 'js': 'text/javascript', 'mjs': 'text/javascript', 'xml': 'text/xml', 'mp4': 'video/mp4', 'm4v': 'video/mp4', 'ogv': 'video/ogg', 'ogm': 'video/ogg', 'csv': 'text/csv', 'ico': 'image/vnd.microsoft.icon'}
def get_http_content_type(extension: str):
132def get_http_content_type(extension: str):
133    """Returns the HTTP Content-Type corresponding to a file extension.
134
135    Returns "application/octet-stream" when the extension is unknown.
136
137    Parameters
138    ----------
139    - extension: str
140        A file extension.
141
142    Returns
143    -------
144    str 
145        An HTTP Content-Type 
146    """
147
148    if file_extension_to_content_type.get(extension) is None:
149        return "application/octet-stream"
150    return file_extension_to_content_type[extension]

Returns the HTTP Content-Type corresponding to a file extension.

Returns "application/octet-stream" when the extension is unknown.

Parameters

  • extension: str A file extension.

Returns

str An HTTP Content-Type