-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiiwServer.py
322 lines (286 loc) · 10.4 KB
/
iiwServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from io import BufferedReader, BufferedWriter, TextIOWrapper
from iiwServerHelper import *
def create404Page(path: str) ->str:
'''This function create the content of a 404 page in which it tells about the path initially asked'''
begin: str= str('''
<html>
<head>
<title>file not found</title>
</head>
<body>
<p> ERROR Request:<b>''')
end: str = str('''</b></p>
<p> has not been found on the server...</p>
</body>
</html>''')
return begin+path+end
def getFileExtension(path: str) -> str:
'''Returns the exntesion of the file pointed by the path'''
# index: int = int(0)
# current: str = str("")
# while (index < len(path)):
# if(path[index] == "."):
# current = ""
# else:
# current = current + path[index]
# index = index + 1
# return current
index: int = int(len(path)-1)
res: str = str("")
while (index >= 0):
if(path[index] == "."):
return res
else:
res = path[index] + res
index = index - 1
return ""
def getContentType(fileExtension: str) -> str:
'''Returns the http content type associated to a given file extension'''
if (fileExtension == "html" or fileExtension == "css" or fileExtension == "csv"):
return "text/"+fileExtension
if (fileExtension == "png" or fileExtension == "jpeg" or fileExtension == "gif" or fileExtension == "webp"):
return "image/"+fileExtension
if (fileExtension == "jpg"):
return "image/jpeg"
if (fileExtension == "txt"):
return "text/plain"
return "unknow type"
def isBinaryFile(path: str) -> bool:
'''Returns True is the file is Binary, False otherwise.
WARNING: this is done based on the file extension and consequently not safe'''
binaryFileExtensions: list[str] = list(["png","jpeg","jpg","gif","webp"])
fileExtension: str = getFileExtension(path)
i : int = int(0)
while (i < len(binaryFileExtensions)):
if (binaryFileExtensions[i] == fileExtension):
return True
i = i + 1
return False
def getPostFilePath(request: str) -> str:
''' returns a string with the path to the post files'''
return "./posts"+request+".txt"
def getTitle(postPath:str) -> str:
''' returns a string of the title of the post at a given path'''
if doesFileExist(postPath):
f: TextIOWrapper = open(postPath, "r")
allLines: list[str] = f.readlines()
return allLines[0]
return "post does not exist"
def getContent(postPath:str) -> str:
''' returns a string of the content of the post at a given path'''
if doesFileExist(postPath):
f: TextIOWrapper = open(postPath, "r")
allLines: list[str] = f.readlines()
res: str = str()
i : int = int(1)
while i< len(allLines):
res = res+"\n"+allLines[i]
i += 1
return res
return "post does not exist"
def addHtmlParagraphs(content:str) -> str:
''' returns a string of the content string with paragraphs tags added'''
i : int = int(0)
res: str = str("<p>")
while i< len(content):
currentCar: str = content[i]
if (currentCar=="\n"):
res = res+"</p>\n<p>"
else:
res = res+currentCar
i += 1
return res+"</p>"
def addHtmlBold(content:str) -> str:
''' returns a string of the content string with bold tags added'''
i : int = int(0)
res: str = str()
insideStars: bool = False
stringSize: int = len(content)
while i<stringSize :
currentChar: str = content[i]
nextChar:str = str("")
if (i < stringSize - 1):
nextChar = content[i+1]
if (currentChar == "*" and nextChar == "*"):
if (not insideStars):
insideStars = True
res = res + "<strong>"
i += 1
else:
insideStars = False
res = res + "</strong>"
i += 1
else:
res = res+currentChar
i += 1
return res
#v2: la version v1 n'a juste pas besoin d'un tagStart tagEnd car les balises sont symétriques.
def addSpecificFormatting(content:str, charToSearch: str, tagStart: str, tagEnd: str) -> str:
''' returns a string of the content where the content between 2 charToSearch is prefixed by tagStart and postfixed with tagEnd'''
content += (" ") # to handle last word in bold or so
i : int = int(0)
res: str = str()
insideChars: bool = False
stringSize: int = len(content)
while i<stringSize :
currentChar: str = content[i]
nextChar:str = str("")
if (i < stringSize - 1):
nextChar = content[i+1]
if (currentChar == charToSearch and nextChar == charToSearch):
if (not insideChars):
insideChars = True
res = res + tagStart
i += 1
else:
insideChars = False
res = res + tagEnd
i += 1
else:
res = res+currentChar
i += 1
return res
def createBlogPage(title: str, content: str) ->str:
'''This function create the content of a blog page in which it put the actual formated content'''
content = addHtmlParagraphs(content)
content = addSpecificFormatting(content, "*", "<strong>", "</strong>")
content = addSpecificFormatting(content, "/", "<i>", "</i>")
content = addSpecificFormatting(content, "#", '<img src="./posts/', '"/>')
return str('''
<html>
<head>
<title>'''+title+'''</title>
<link href="./static/css/iiwBlogStyle.css" rel="stylesheet"/>
</head>
<body>
<div class="main" id="main">
<ol class="menu">
<li><a href="/">home</a></li>
</ol>
<h1>'''+title+'''</h1>
'''+content+'''
</div>
</body>
</html>''')
def handleBadRequest(path) -> None:
'''create and send a 404 page'''
sendResponse(404)
sendHeader("Content-type", "text/html")
sendTextualFileContent(create404Page(path))
def handleFileBasedRequest(path):
'''serve an existing file'''
prefix: str = str('.')
sendResponse(200)
sendHeader("Content-type", getContentType(path))
if(not isBinaryFile(prefix+path)):
tFile: TextIOWrapper = open(prefix+path,"r")
tContent : str = tFile.read()
sendTextualFileContent(tContent)
else:
bFile: BufferedReader = open(prefix+path,"rb")
bcontent : bytes = bFile.read()
sendBinaryFileContent(bcontent)
def handleBlogPagesRequest(path, postFilePath):
'''serve a blog post textual file after formatting'''
postTitle: str = getTitle(postFilePath)
postContent: str = getContent(postFilePath)
htmlContent: str = createBlogPage(postTitle,postContent)
sendResponse(200)
sendHeader("Content-type", getContentType(path))
sendTextualFileContent(htmlContent)
def removeExtension(filePath:str) -> str:
fileExtension: str = getFileExtension(filePath)
i: int = int(0)
res: str = str()
while i < (len(filePath) - len(fileExtension) - 1):
res += filePath[i]
i += 1
return res
def createIndexPage() -> str:
allFilePath: list[str] = list()
allTitles: list[str] = list()
for postPath in (os.listdir("./posts/")):
if (getFileExtension(postPath) == "txt"):
allFilePath.append(removeExtension(postPath))
allTitles.append(getTitle("./posts/"+postPath))
allLinks: list[str] = list()
i: int = int(0)
while i < len(allFilePath):
allLinks.append('<a href="'+allFilePath[i]+'">'+allTitles[i]+'</a>')
i += 1
allLinksAsHtmlList: str = str('<ul>\n')
i = 0
while i < len(allLinks):
allLinksAsHtmlList += '<li>'+allLinks[i]+'</li>\n'
i += 1
allLinksAsHtmlList += '</ul>\n'
return str('''
<html>
<head><title>The best blog ever</title></head>
<link href="./static/css/iiwBlogStyle.css" rel="stylesheet"/>
</head>
<body>
<div class="main" id="main">
<ol class="menu">
<li><a href="./static/html/addPost.html">add a post</a></li>
</ol>
<h1> welcome on the IIW blog ! </h1>
<h2>here is the list of posts we have so far </h2>
<h3>'''+allLinksAsHtmlList+'''</h3>
</div>
</body>
</html>
''')
def handleGetRequest(path: str) -> None:
'''This function defines how my server handle the get requests'''
if(path == "/" or path=="/index.html"):
sendResponse(200)
sendHeader("Content-type", "text/html")
sendTextualFileContent(createIndexPage())
return
if (getFileExtension(path)==""):
postFilePath: str = getPostFilePath(path)
if (not doesFileExist(postFilePath)):
handleBadRequest(path)
return
handleBlogPagesRequest(path, postFilePath)
return
if not doesFileExist("."+path):
handleBadRequest(path)
return
handleFileBasedRequest(path)
nbPost: int = int(0)
def handlePostRequest(incomingData: list[PostedData]) -> None:
global nbPost
title: str = str()
content: str = str()
picture: bytes = bytes()
pictureName: str = str()
for p in incomingData:
if (p.name=="title"):
title = p.value
elif(p.name == "content"):
content = p.value
elif(p.name == "picture"):
picture = p.value # type: ignore
elif(p.name == "pictureName"):
pictureName = p.value
nbPost += 1
postName: str = str("/Post"+str(nbPost))
postFilePath: str = getPostFilePath(postName)
file: TextIOWrapper = open(postFilePath, "w")
file.write(title+"\n")
file.write(content)
file.close()
pFile: BufferedWriter = open("./posts/"+pictureName, "wb")
print("picture type",type(picture))
pFile.write(picture)
pFile.close
handleBlogPagesRequest(postName, postFilePath)
#test server
nbPost = len(os.listdir("./posts/"))
setHandleGetRequest(handleGetRequest)
setHandlePostRequest(handlePostRequest)
hostName = "localhost"
serverPort = 8080
launchServer(hostName,serverPort)