src.myserver.file

A package for learning network programming in Python.

This module (file) manages operations relative to the file system.

  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) manages operations relative to the file system.
 15"""
 16
 17import os.path as path
 18
 19
 20def resolve_location(res:str, root: str):
 21    """Returns the path of a resource relative to the root and its extension.
 22
 23    Returns ("", "") if the concatenated path does not exist.
 24
 25    "index.html" is appended to directory paths.
 26
 27    Parameters
 28    ----------
 29    res: str 
 30        The queried resource path.
 31    root: str 
 32        The root directory where to look into for res.
 33
 34    Returns
 35    -------
 36    str
 37        The full disk path of the resource if it exists, or "".
 38    str
 39        The extension of the resource if it exists, or "".
 40    """
 41    try:
 42        res_path = resolve_path(res, root)
 43
 44        # Compute file extension
 45        extension = path.splitext(res_path)[1]
 46        # Maybe remove leading '.'
 47        if len(extension) > 0 and extension[0] == '.':
 48            extension = extension[1:]
 49
 50        return res_path, extension
 51    except Exception: 
 52        return "", ""
 53
 54def resolve_path(res:str, root: str):
 55    """Returns the full disk path of a resource relative to the root.
 56
 57    Returns "" if the concatenated path does not exist.
 58
 59    "index.html" is appended to directory paths.
 60
 61    Parameters
 62    ----------
 63    res: str 
 64        The queried resource path.
 65    root: str 
 66        The root directory where to look into for res.
 67
 68    Returns
 69    -------
 70    str
 71        The full disk path of the resource if it exists, or "".
 72    """
 73    try:
 74        # Resolve the home directory if it is in the root
 75        root = path.expanduser(root)
 76
 77        if res[-1] == "/":
 78            res += "index.html"
 79        while res[0] == "/":
 80            res = res[1:]
 81
 82        res_path = path.join(root, res)
 83        
 84        if not (path.exists(res_path) and path.isfile(res_path)):
 85            return ""
 86        return res_path
 87    except Exception: 
 88        return ""
 89
 90
 91
 92def get_resource(res_path: str):
 93    """Returns a resource at res_path, its content type and an HTTP code.
 94
 95    Parameters
 96    ----------
 97    - res_path: str
 98        Requested resource string.
 99
100    Returns
101    -------
102    bytes
103        The resource content if it exists (code == 200).
104    int 
105        A HTTP status code.
106    """
107
108
109    print(f"Opening file {res_path}...")
110    with open(res_path, 'rb') as f:
111        content = f.read()
112        return content, 200
def resolve_location(res: str, root: str):
21def resolve_location(res:str, root: str):
22    """Returns the path of a resource relative to the root and its extension.
23
24    Returns ("", "") if the concatenated path does not exist.
25
26    "index.html" is appended to directory paths.
27
28    Parameters
29    ----------
30    res: str 
31        The queried resource path.
32    root: str 
33        The root directory where to look into for res.
34
35    Returns
36    -------
37    str
38        The full disk path of the resource if it exists, or "".
39    str
40        The extension of the resource if it exists, or "".
41    """
42    try:
43        res_path = resolve_path(res, root)
44
45        # Compute file extension
46        extension = path.splitext(res_path)[1]
47        # Maybe remove leading '.'
48        if len(extension) > 0 and extension[0] == '.':
49            extension = extension[1:]
50
51        return res_path, extension
52    except Exception: 
53        return "", ""

Returns the path of a resource relative to the root and its extension.

Returns ("", "") if the concatenated path does not exist.

"index.html" is appended to directory paths.

Parameters

res: str The queried resource path. root: str The root directory where to look into for res.

Returns

str The full disk path of the resource if it exists, or "". str The extension of the resource if it exists, or "".

def resolve_path(res: str, root: str):
55def resolve_path(res:str, root: str):
56    """Returns the full disk path of a resource relative to the root.
57
58    Returns "" if the concatenated path does not exist.
59
60    "index.html" is appended to directory paths.
61
62    Parameters
63    ----------
64    res: str 
65        The queried resource path.
66    root: str 
67        The root directory where to look into for res.
68
69    Returns
70    -------
71    str
72        The full disk path of the resource if it exists, or "".
73    """
74    try:
75        # Resolve the home directory if it is in the root
76        root = path.expanduser(root)
77
78        if res[-1] == "/":
79            res += "index.html"
80        while res[0] == "/":
81            res = res[1:]
82
83        res_path = path.join(root, res)
84        
85        if not (path.exists(res_path) and path.isfile(res_path)):
86            return ""
87        return res_path
88    except Exception: 
89        return ""

Returns the full disk path of a resource relative to the root.

Returns "" if the concatenated path does not exist.

"index.html" is appended to directory paths.

Parameters

res: str The queried resource path. root: str The root directory where to look into for res.

Returns

str The full disk path of the resource if it exists, or "".

def get_resource(res_path: str):
 93def get_resource(res_path: str):
 94    """Returns a resource at res_path, its content type and an HTTP code.
 95
 96    Parameters
 97    ----------
 98    - res_path: str
 99        Requested resource string.
100
101    Returns
102    -------
103    bytes
104        The resource content if it exists (code == 200).
105    int 
106        A HTTP status code.
107    """
108
109
110    print(f"Opening file {res_path}...")
111    with open(res_path, 'rb') as f:
112        content = f.read()
113        return content, 200

Returns a resource at res_path, its content type and an HTTP code.

Parameters

  • res_path: str Requested resource string.

Returns

bytes The resource content if it exists (code == 200). int A HTTP status code.