<commit_before>(in-package #:laap/test)

;;; Run with: ./buildapp --output bin/foo --eval '(declaim (optimize (speed 3)))' --asdf-tree ~/quicklisp/ --load-system laap-test --entry laap/test::main --eval '(setf *debugger-hook* (lambda (c h) (declare (ignore h)) (format t "~A~%" c)))'
;;; for strace runs et al.
(defun main (&rest args)
  (declare (ignore args))
  (laap:with-event-loop
    (http-request (lambda (result)
		    (format t "~a" result)))))

(defun accept-loop (socket)
  (let ()
    (lambda (&optional err res)
      (when err (error err))
      (laap/socket:accept
       socket
       (accept-fn socket)))))

(defun accept-fn (socket)
  (let ()
    (lambda (err client-socket)
      (laap:spawn (accept-loop socket))
      (when err (error err))
      (laap/socket:send
       client-socket
       (lambda (err res)
	 (when err (error err))
	 (laap/socket:close
	  client-socket
	  #'laap:noop))
       :data (babel:string-to-octets (format nil
					     "HTTP/1.0 200 OK~c~cContent-Length: 6~c~c~c~cPong!~c~c~c"
					     #\return #\linefeed
					     #\return #\linefeed #\return #\linefeed
					     #\linefeed
					     #\return #\linefeed))))))

(defun http-request (done)
  (let ((socket (make-instance 'laap/socket:ipv4-socket)))
    (laap/socket:connect
     socket
     (lambda (err res)
       (when err (error err))
       (laap/socket:send
	socket
	(lambda (err result)
	  (when err (error err))
	  (let ((result ""))
	    (laap/socket:receive
	     socket
	     (lambda (err res)
	       (when err (error err))
	       (setf result (concatenate 'string result (babel:octets-to-string res))))
	     :end (lambda (err res)<commit_msg>Add some tests.<commit_after>(in-package #:laap/test)

;;; Run with: ./buildapp --output bin/foo --eval '(declaim (optimize (speed 3)))' --asdf-tree ~/quicklisp/ --load-system laap-test --entry laap/test::http-client --eval '(setf *debugger-hook* (lambda (c h) (declare (ignore h)) (format t "~A~%" c)))'
;;; for strace runs et al.
(defun http-client (&rest args)
  (declare (ignore args))
  (laap:with-event-loop
    (http-request (lambda (result)
		    (format t "~a" result)))))

;;; ./buildapp --output bin/foo --eval '(declaim (optimize (speed 3)))' --asdf-tree ~/quicklisp/ --load-system laap-test --entry laap/test::http-server --eval '(setf *debugger-hook* (lambda (c h) (declare (ignore h)) (format t "~A~%" c)))'
(defun http-server (&rest args)
  (declare (ignore args))
  (laap:with-event-loop
    (let ((socket (make-instance 'laap/socket:ipv4-socket)))
      (laap/socket:listen socket (accept-loop socket)
			  :ip "127.0.0.1" :port 9092))))

(defun accept-loop (socket)
  (let ()
    (lambda (&optional err res)
      (when err (error err))
      (laap/socket:accept
       socket
       (accept-fn socket)))))

(defun accept-fn (socket)
  (let ()
    (lambda (err client-socket)
      (laap:spawn (accept-loop socket))
      (when err (error err))
      (laap/fs:open
       "/etc/hosts"
       (lambda (err file)
	 (when err (error err))
	 (laap/fs:read
	  file
	  (lambda (err hosts)
	    (when err (error err))
	    (laap/socket:send
	     client-socket
	     (lambda (err res)
	       (declare (ignore res))
	       (when err (error err))
	       (laap/socket:close
		client-socket
		#'laap:noop))
	     :data (babel:string-to-octets
		    (format nil
			    "HTTP/1.0 200 OK~c~cContent-Length: ~a~c~c~c~c~a~c~c"
			    #\return #\linefeed
			    (length hosts)
			    #\return #\linefeed #\return #\linefeed
			    (babel:octets-to-string hosts)
			    #\return #\linefeed))))
	  :count 4096))))))

(defun http-request (done)
  (let ((socket (make-instance 'laap/socket:ipv4-socket)))
    (laap/socket:connect
     socket
     (lambda (err res)
       (when err (error err))
       (laap/socket:send
	socket
	(lambda (err result)
	  (when err (error err))
	  (let ((result ""))
	    (laap/socket:receive
	     socket
	     (lambda (err res)
	       (when err (error err))
	       (setf result (concatenate 'string result (babel:octets-to-string res))))
	     :end (lambda (err res)
{'fraction_non_alphanumeric': 0.17105587004676348, 'fraction_numerical': 0.007629830174747723, 'mean_word_length': 2.70464904284412, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5722307', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1430, 'n_words': 388}