1
0
Innocenty Enikeew преди 10 години
родител
ревизия
252b7f3927
променени са 1 файла, в които са добавени 16 реда и са изтрити 9 реда
  1. 16 9
      utils.lisp

+ 16 - 9
utils.lisp

@@ -93,17 +93,24 @@ is replaced with replacement."
 
 ;; XML processing
 (defun xml-request (url)
-  (multiple-value-bind (stream status headers uri http-stream)
+  (multiple-value-bind (raw-body status headers uri http-stream)
       (drakma:http-request (http-default url)
-                           :want-stream t
+                           :force-binary t
                            :decode-content t)
-    (declare (ignore status headers))
-    (unwind-protect
-         (progn
-           (when (eq (flex:flexi-stream-element-type stream) 'octet)
-             (setf (flex:flexi-stream-external-format stream) :utf-8))
-           (values (plump:parse stream) uri))
-      (ignore-errors (close http-stream)))))
+    (declare (ignore status headers http-stream))
+    (let* ((dom
+            (handler-case
+                (plump:parse (flex:octets-to-string raw-body :external-format :utf-8))
+              (flex:external-format-encoding-error ()
+                (plump:parse (flex:octets-to-string raw-body)))))
+           (encoding (plump:get-attribute (plump:first-child dom) "encoding")))
+      (values
+       (if (and encoding (not (equal encoding "utf-8")))
+           (plump:parse (flex:octets-to-string
+                         raw-body
+                         :external-format (intern encoding 'keyword)))
+           dom)
+       uri))))
 
 (defun get-by-tag (node tag)
   (nreverse (plump::get-elements-by-tag-name node tag)))