-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuri-parse.lisp
475 lines (429 loc) · 16.5 KB
/
uri-parse.lisp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
;872491 - Pulcino Federico
; Sezione identificatori
(defun helper-identificatore
(lista &optional listaDelimitatori listaBannati accumulatore)
(cond ((null lista) nil)
((member (car lista) listaBannati) nil)
((member (car lista) listaDelimitatori)
(values (nreverse accumulatore) lista))
(T (helper-identificatore
(cdr lista)
listaDelimitatori
listaBannati
(cons (car lista) accumulatore)))))
(defun identificatore
(lista &optional listaDelimitatori listaBannati)
(let ((helperReturn
(multiple-value-list
(helper-identificatore lista listaDelimitatori listaBannati))))
(if (first helperReturn)
(values-list helperReturn)
(values nil lista))))
(defun helper-id-port (lista &optional listaDelimitatori accumulatore)
(cond ((null lista) nil)
((member (car lista) listaDelimitatori)
(values (nreverse accumulatore) lista))
((null (digit-char-p (car lista))) (error "port solo numeri"))
(T (helper-id-port (cdr lista)
listaDelimitatori
(cons (car lista) accumulatore)))))
(defun identificatore-port (lista &optional listaDelimitatori)
(let ((helperReturn
(multiple-value-list
(helper-id-port lista listaDelimitatori))))
(if (first helperReturn)
(values-list helperReturn)
(values 80 lista))))
; fine sezione identificatori
; Sezione Subdomain
(defun fragment (lista)
(cond ((eq (car lista) #\#)
(multiple-value-bind
(parsedFragment remaining)
(identificatore (cdr lista) (list 'end))
(values (coerce parsedFragment 'string) remaining)))
(T (values nil lista))))
(defun query (lista)
(cond ((eq (car lista) #\?)
(multiple-value-bind
(parsedQuery remaining)
(identificatore (cdr lista) (list #\# 'end))
(values (coerce parsedQuery 'string) remaining)))
(T (values nil lista))))
(defun path (lista)
(multiple-value-bind
(parsedPath remaining)
(identificatore lista (list #\/ #\? #\# 'end) (list #\@ #\:))
(cond ((null parsedPath) (values nil remaining))
((and (eq (car remaining) #\/)
(eq (second remaining) 'end))
(error "Errore path"))
((and (eq (car remaining) #\/)
(eq (second remaining) 'end))
(error "errore path"))
((and (eq (car remaining) #\/)
(not (member (second remaining) (list #\# #\@ #\: #\/ #\?))))
(multiple-value-bind
(parsedSubPath subRemaining)
(path (cdr remaining))
(values (concatenate 'string parsedPath '(#\/) parsedSubPath)
subRemaining)))
((and (eq (car remaining) #\/)
(member (second remaining) (list #\# #\@ #\: #\/ #\?)))
(error "Errore path"))
(T (values (coerce parsedPath 'string) remaining)))))
; Struttura subdomain
(defun subdomain (lista)
(cond ((eq (car lista) #\/)
(let* (
(Path
(multiple-value-list
(path (cdr lista))))
(Query
(cond ((and (eq (car (second Path)) #\?)
(member (second (second Path)) (list #\# 'end)))
(error "Errore query"))
(T (multiple-value-list (query (second Path))))))
(Fragment
(cond ((and (eq (car (second Query)) #\#)
(eq (second (second Query)) 'end))
(error "Errore fragment"))
(T (multiple-value-list (fragment (second Query)))))))
(cond ((eq (car (second Fragment)) 'end)
(values (first Path) (first Query) (first Fragment)))
(T (error "Errore subdomain")))))
(T (values nil nil nil))))
; Fine sezione Subdomain
; Sezione Authority
(defun port (lista)
(cond ((eq (car lista) #\:)
(multiple-value-bind
(parsedPort remaining)
(identificatore-port (cdr lista) (list #\/ 'end))
(values (parse-integer (coerce parsedPort 'string)) remaining)))
(T (values 80 lista))))
(defun helper-ip (lista &optional final)
(multiple-value-bind
(parsedTriplet remaining)
(identificatore lista (list #\. #\: #\/ 'end))
(cond ((or (> (parse-integer (coerce parsedTriplet 'string)) 255)
(< (parse-integer (coerce parsedTriplet 'string)) 0))
(error "ip non valido"))
((and final (member (car remaining) (list #\. #\? #\@ #\#)))
(error "errore fine ip"))
((and (not final) (not (eq (car remaining) #\.)))
(error "errore punto ip"))
(T (values (coerce parsedTriplet 'string) remaining)))))
(defun ip (lista)
(let* ((firstTriplet
(multiple-value-list
(helper-ip lista)))
(secondTriplet
(multiple-value-list
(helper-ip (cdr (second firstTriplet)))))
(thirdTriplet
(multiple-value-list
(helper-ip (cdr (second secondTriplet)))))
(fourthTriplet
(multiple-value-list
(helper-ip (cdr (second thirdTriplet)) T))))
(values (append (first firstTriplet) '(#\.)
(first secondTriplet)'(#\.)
(first thirdTriplet) '(#\.)
(first fourthTriplet)) (second fourthTriplet))))
(defun host (lista)
(multiple-value-bind
(parsedHost remaining)
(identificatore lista (list #\. #\/ #\: 'end) (list #\? #\# #\@))
(cond ((and (eq (car remaining) #\.) (eq (second remaining) #\.))
(error "errore host (due punti consecutivi)"))
((eq (length parsedHost) 0) (error "host obbligatorio"))
((eq (car remaining) #\.)
(multiple-value-bind
(parsedSubHost subRemaining)
(host (cdr remaining))
(cond ((eq (length parsedSubHost) 0)
(error "subHost non deve essere nullo"))
(T (values (concatenate 'string parsedHost '(#\.) parsedSubHost)
subRemaining)))))
(T (values (coerce parsedHost 'string) remaining)))))
(defun helper-host (lista)
(handler-case (ip lista)
(error ()
(host lista))))
(defun userinfo (lista)
(multiple-value-bind
(parsedUserinfo remaining)
(identificatore lista (list #\@) (list #\/ #\? #\# #\:))
(cond ((null parsedUserinfo) (values nil remaining))
(T (values (coerce parsedUserinfo 'string) (cdr remaining))))))
(defun authority (lista)
(cond ((and
(eq (first lista) #\/)
(eq (second lista) #\/))
(let* ((Userinfo
(multiple-value-list
(userinfo (cdr (cdr lista)))))
(Host
(multiple-value-list
(helper-host (second Userinfo))))
(Port
(cond ((eq (length (first Host)) 0) (error "Host obbligatorio"))
(T (multiple-value-list
(port (second Host)))))))
(values (first Userinfo) (first Host) (first Port) (second Port))))
((member (third lista) (list #\/ #\? #\# #\@ #\:))
(error "carattere non ammesso"))
((eq (car lista) #\/) (values nil nil 80 lista))
((eq (car lista) 'end) (values nil nil 80 nil nil nil 'end))
(T (error "Uri non valido"))))
; Fine sezione Authority
; Struttura URI
(defun helper-uri (lista)
(let* ((Authority
(multiple-value-list
(authority lista)))
(Subdomain
(cond ((eq (fourth Authority) 'end)
(values nil nil nil))
(T (multiple-value-list
(subdomain (fourth Authority))))))
(helpUriReturn
(append (list (first Authority)
(second Authority)
(third Authority))
Subdomain)))
helpUriReturn))
; Sezione Schemi speciali
; ZOS
(defun zos-id8-helper
(lista &optional listaDelimitatori listaBannati accumulatore)
(cond ((null lista) nil)
((member (car lista) listaBannati) (error "carattere non valido id8"))
((member (car lista) listaDelimitatori)
(values (nreverse accumulatore) lista))
((eq (car lista) #\SPACE) (error "spazio non valido"))
((not (alphanumericp (car lista))) (error "id8 solo alphanumeric char"))
(T (zos-id8-helper
(cdr lista)
listaDelimitatori
listaBannati
(cons (car lista) accumulatore)))))
(defun identificatore-id8 (lista &optional listaDelimitatori listaBannati)
(let ((helperReturn
(multiple-value-list
(zos-id8-helper lista listaDelimitatori listaBannati))))
(if (first helperReturn) (values-list helperReturn) (values nil lista))))
(defun zos-id44-helper
(lista &optional listaDelimitatori listaBannati accumulatore)
(cond ((null lista) nil)
((member (car lista) listaBannati) (error "carattere non valido id44"))
((member (car lista) listaDelimitatori)
(values (nreverse accumulatore) lista))
((eq (car lista) #\SPACE) (error "spazio non valido in id44"))
((and (not (alphanumericp (car lista))) (not(eq (car lista) #\.)))
(error "id44 solo alphanumeric char o ."))
(T (zos-id44-helper
(cdr lista)
listaDelimitatori
listaBannati
(cons (car lista) accumulatore)))))
(defun identificatore-id44 (lista &optional listaDelimitatori listaBannati)
(let ((helperReturn
(multiple-value-list
(zos-id44-helper lista listaDelimitatori listaBannati))))
(if (first helperReturn) (values-list helperReturn) (values nil lista))))
(defun id44 (lista)
(if (alpha-char-p (car lista))
(multiple-value-bind
(parsedID44 remaining)
(identificatore-id44 lista (list #\? #\# #\( 'end))
(cond ((eq (car (last parsedID44)) #\.)
(error "ID44 non può terminare con ."))
((> (list-length parsedID44) 44) (error "ID44 troppo lungo"))
(T (values (coerce parsedID44 'string) remaining))))
(error "id44 deve iniziare con un carttere alfabetico")))
(defun id8 (lista)
(if (alpha-char-p (car lista))
(multiple-value-bind
(parsedID8 remaining)
(identificatore-id8 lista (list #\) 'end) (list #\. #\? #\# 'end))
(if (> (list-length parsedID8 ) 8)
(error "ID8 troppo lungo")
(values (coerce parsedID8 'string) remaining)))
(error "id8 deve iniziare con un carattere afabetico")))
(defun helper-id8 (lista)
(cond ((eq (car lista) #\()
(id8 (cdr lista)))
(T (values nil lista))))
(defun zos-path (lista)
(cond ((member (second lista) (list #\? #\# #\( 'end))
(error "id44 obbligatorio"))
(T (let* ((ID44
(multiple-value-list
(id44 (cdr lista))))
(ID8
(multiple-value-list
(helper-id8 (second ID44)))))
(cond ((null (first ID8)) (values (first ID44) (second ID8)))
(T (values (concatenate 'string (first ID44)
"(" (first ID8) ")")
(cdr (second ID8)))))))))
(defun zos (lista)
(let* ((Authority
(multiple-value-list
(authority lista)))
(Path
(multiple-value-list
(zos-path (fourth Authority))))
(Query
(cond ((and (eq (car (second Path)) #\?)
(member (second (second Path)) (list #\# 'end)))
(error "Errore query"))
(T (multiple-value-list
(query (second Path))))))
(Fragment
(cond ((and (eq (car (second Query)) #\#)
(eq (second (second Query)) 'end))
(error "Errore fragment"))
(T (multiple-value-list
(fragment (second Query))))))
(zosReturn
(values (append (list (first Authority))
(list (second Authority))
(list (third Authority))
(list (first Path))
(list (first Query))
(list (first Fragment))))))
zosReturn))
; Tel&fax
(defun telfax (lista)
(multiple-value-bind
(parsedTelFax remaining)
(identificatore lista (list 'end) (list #\/ #\? #\# #\@ #\:))
(cond ((not (member (first remaining) (list #\: 'end)))
(error "telfax non valido"))
(T (values (list (coerce parsedTelFax 'string) nil 80 nil nil nil))))))
; News
(defun news (lista)
(multiple-value-bind
(parsedNews remaining)
(handler-case (host lista)
(error ()
(values nil lista)))
(cond
((and (null parsedNews) (eq (first remaining) 'end))
(values (list nil nil 80 nil nil nil)))
((not (eq (first remaining) 'end)) (error "news non valido"))
(T (values (list nil (coerce parsedNews 'string) 80 nil nil nil))))))
; Mailto
(defun mail-host (lista)
(multiple-value-bind
(parsedHost remaining)
(identificatore lista (list #\. 'end) (list #\? #\# #\@ #\/ #\:))
(cond ((and (eq (car remaining) #\.) (eq (second remaining) #\.))
(error "errore host"))
((null parsedHost) (error "host obbligatorio"))
((eq (car remaining) #\.)
(multiple-value-bind
(parsedSubHost subRemaining)
(host (cdr remaining))
(values (concatenate 'string parsedHost '(#\.) parsedSubHost)
subRemaining)))
(T (values (coerce parsedHost 'string))))))
(defun mail-userinfo (lista)
(multiple-value-bind
(parsedMail remaining)
(identificatore lista (list #\@ 'end) (list #\/ #\? #\# #\:))
(cond ((eq (car lista) 'end) (values nil lista))
(T (values (coerce parsedMail 'string) remaining)))))
(defun mailto (lista)
(let* ((Userinfo
(multiple-value-list
(mail-userinfo lista)))
(Host
(cond ((and (eq (length (first Userinfo)) 0)
(not (eq (first (second Userinfo)) 'end)))
(error "mailto non valido"))
((and (equal (first (second Userinfo)) #\@)
(eq (second (second Userinfo)) 'end))
(error "mailto non valido"))
((equal (first (second Userinfo)) #\@)
(multiple-value-list
(mail-host (cdr (second Userinfo)))))
(T (values nil))))
(mailReturn
(append (list (first Userinfo))
(list (first Host))
(list 80 nil nil nil))))
mailReturn))
; Fine schemi speciali
; Riconoscimento scheme
(defun scheme (lista)
(multiple-value-bind
(parsedScheme remaining)
(identificatore lista (list #\:) (list #\/ #\? #\# #\@))
(values (coerce parsedScheme 'string) (cdr remaining))))
(defun helper-scheme (lista)
(multiple-value-bind
(parsedScheme remaining)
(scheme lista)
(cond ((eq (length parsedScheme) 0) (error "Scheme obbligatorio"))
((string-equal parsedScheme "mailto")
(values (append (list parsedScheme)
(mailto remaining))))
((equal parsedScheme "news")
(values (append (list parsedScheme) (news remaining))))
((or (equal parsedScheme "tel") (equal parsedScheme "fax"))
(values (append (list parsedScheme) (telfax remaining))))
((equal parsedScheme "zos")
(values (append (list parsedScheme) (zos remaining))))
(T (values (append (list parsedScheme) (helper-uri remaining)))))))
; fine riconoscimento scheme
; funzione iniziale uri-parse
(defun uri-parse (uriString)
(handler-case (helper-scheme (append (coerce uriString 'list) (list 'end)))
(error ()
nil)))
; GETTER singoli elementi URI da struttura
; get scheme
(defun uri-scheme (lista)
(cond ((eq (length (first lista)) 0) nil)
(T (first lista))))
; get userinfo
(defun uri-userinfo (lista)
(cond ((eq (length (second lista)) 0) nil)
(T (second lista))))
; get host
(defun uri-host (lista)
(cond ((eq (length (third lista)) 0) nil)
(T (third lista))))
; get port
(defun uri-port (lista)
(fourth lista))
; get path
(defun uri-path (lista)
(cond ((eq (length (fifth lista)) 0) nil)
(T (fifth lista))))
; get query
(defun uri-query (lista)
(cond ((eq (length (sixth lista)) 0) nil)
(T (sixth lista))))
; get fragment
(defun uri-fragment (lista)
(cond ((eq (length (seventh lista)) 0) nil)
(T (seventh lista))))
; fine Getters
; funzione uri-display
(defun uri-display (lista &optional (stream t))
(format stream
"~%Scheme: ~S~%Userinfo: ~S~%Host: ~S~%Port: ~D~%"
(uri-scheme lista)
(uri-userinfo lista)
(uri-host lista)
(uri-port lista))
(format stream
"Path: ~S~%Query: ~S~%Fragment: ~S"
(uri-path lista)
(uri-query lista)
(uri-fragment lista)))