-
Notifications
You must be signed in to change notification settings - Fork 30
/
playback.cl
150 lines (104 loc) · 3.54 KB
/
playback.cl
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
;; -*- mode: common-lisp; package: net.aserve -*-
;;
;; playback.cl
;;
;; See the file LICENSE for the full license governing this code.
;;
;;
;; Description:
;; playback a script generated by logging a site
;;- This code in this file obeys the Lisp Coding Standard found in
;;- http://www.franz.com/~jkf/coding_standards.html
;;-
;; 2010-12-08 mm: This file is not included in the build script for the
;; production release.
;; The code in playback-form assumes only one thread will be calling it
;; at any instant. Use by several threads will require adding some locks.
(in-package :net.aserve)
(eval-when (compile) (declaim (optimize (speed 3))))
(defvar *last-responses* nil)
(defparameter *debug-playback* nil)
(defun playback (server filename)
(with-open-file (p filename :direction :input)
(do ((form (read p nil nil) (read p nil nil))
(jar (make-instance 'net.aserve.client::cookie-jar)))
((null form))
(playback-form server form jar))))
(defun playback-form (server form jar)
(macrolet ((qval (tag) `(cdr (assoc ,tag form :test #'equal))))
(let ((method (qval :method))
(uri (qval :uri))
(code (qval :code))
(auth (qval :auth))
(body (qval :body)))
;; special hack to handle a few cases
(if* (and body (match-re "user-id=" body))
then ; must do the hack
(multiple-value-bind (user-id call-id)
(find-user-id-etc)
(if* user-id
then
(setq body
(concatenate 'string
(format nil "user-id=~a&call-id=~a&~a"
user-id
call-id
(remove-regexp
"user-id=[^&]+&"
(remove-regexp
"call-id=[^&]*&"
body)))))
(and *debug-playback* (format t "~%~%new body ~s~%~%" body))
)))
;;
(if* (eql 401 code)
then ; authorization needed
(format t "auth failed, skipping ~s~%" uri)
(return-from playback-form nil))
(and *debug-playback* (format t "do ~s ~s~%" method uri))
(multiple-value-bind (body retcode headers)
(net.aserve.client:do-http-request
(format nil "~a~a" server uri)
:method method
:content (and (eq method :post)
body)
:content-type (and (eq method :post)
(qval :ctype))
:basic-authorization auth
:cookies jar)
(declare (ignore headers))
(push body *last-responses*)
(and *debug-playback*
(format t "ret ~s length(body) ~s~%" retcode (length body)))))))
(defun find-user-id-etc ()
(dolist (resp *last-responses*)
(multiple-value-bind (ok whole call-id)
(match-re "name=\"call-id\" value=\"(.*?)\""
resp
:multiple-lines t
:case-fold t)
(declare (ignore whole))
(if* ok
then (and *debug-playback* (format t "new call id is ~s~%" call-id))
else (and *debug-playback* (format t "No call id~%"))
(go out))
(multiple-value-bind (ok whole user-id)
(match-re "name=\"user-id\" value=\"(.*?)\""
resp
:case-fold t
:multiple-lines t)
(declare (ignore whole))
(if* ok
then (and *debug-playback* (format t "new user id is ~s~%" user-id))
else (and *debug-playback* (format t "No call id in ~s~%" resp))
(go out))
(return (values user-id call-id))))
out
))
(defun remove-regexp (regexp string)
(multiple-value-bind (ok whole before after)
(match-re (format nil "^(.*)~a(.*)$" regexp) string)
(declare (ignore whole))
(if* ok
then (concatenate 'string before after)
else string)))