commit d39dc50c7a0496029a7e510c5b627203082fec3d
parent c97bd9aa46d53200da7e12a92e7e532eef541ca9
Author: Tomas Hlavaty <tom@logand.com>
Date: Sun, 18 Aug 2013 01:05:03 +0200
curl and wget refactored from cl-ipp
Diffstat:
M | cl-rw.asd | | | 3 | ++- |
A | net.lisp | | | 74 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 76 insertions(+), 1 deletion(-)
diff --git a/cl-rw.asd b/cl-rw.asd
@@ -39,4 +39,5 @@
(:file "filesystem")
(:file "base64")
(:file "xml")
- (:file "email")))
+ (:file "email")
+ (:file "net")))
diff --git a/net.lisp b/net.lisp
@@ -0,0 +1,74 @@
+;;; Copyright (C) 2013 Tomas Hlavaty <tom@logand.com>
+;;;
+;;; Permission is hereby granted, free of charge, to any person
+;;; obtaining a copy of this software and associated documentation
+;;; files (the "Software"), to deal in the Software without
+;;; restriction, including without limitation the rights to use, copy,
+;;; modify, merge, publish, distribute, sublicense, and/or sell copies
+;;; of the Software, and to permit persons to whom the Software is
+;;; furnished to do so, subject to the following conditions:
+;;;
+;;; The above copyright notice and this permission notice shall be
+;;; included in all copies or substantial portions of the Software.
+;;;
+;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+;;; DEALINGS IN THE SOFTWARE.
+
+(defpackage :rw.net
+ (:use :cl)
+ (:export :curl
+ :wget))
+
+(in-package :rw.net)
+
+(defun run-command (cmd args &optional error-plist)
+ (let ((code
+ #+ccl(ccl::external-process-%exit-code (ccl:run-program cmd args))
+ #-ccl(error "TODO port IPP.WGET::RUN-COMMAND")))
+ (unless (zerop code)
+ (let ((reason (or (cdr (assoc code error-plist)) "")))
+ (error (format nil "~a error ~d: ~a ~s" cmd code reason args))))))
+
+(defun wget (url &key request-file response-file content-type)
+ (run-command
+ "wget"
+ `("-q"
+ ,@ (when request-file
+ `("--post-file" ,(namestring request-file)))
+ ,@ (when response-file
+ `("-O" ,(namestring response-file)))
+ ,@ (when content-type
+ `("--header" ,(format nil "Content-Type:~a" content-type)))
+ ,url)
+ '((1 . "Generic panic code")
+ (2 . "Parse panic")
+ (3 . "File I/O panic")
+ (4 . "Network failure")
+ (5 . "SSL verification failure")
+ (6 . "Username/password authentication failure")
+ (7 . "Protocol panics")
+ (8 . "Server issued an panic response"))))
+
+;;(wget "http://localhost:631/printers/" :response-file "/tmp/a.html")
+;;(rw.xml:parse-xml #p"/tmp/a.html")
+
+(defun curl (url &key request-file response-file content-type)
+ (run-command
+ "curl"
+ `("-s"
+ ,@ (when request-file
+ `("--data-binary" ,(format nil "@~a" request-file)))
+ ,@ (when response-file
+ `("-o" ,response-file))
+ ,@ (when content-type
+ `("-H" ,(format nil "Content-Type:~a" content-type)))
+ ,url)))
+
+;;(curl "http://localhost:631/printers/" :response-file "printers.html")
+;;(curl "http://localhost:631/jobs/82" :response-file "job-status.html")