skeeto / impatient-mode

Impatient html mode. See your changes in the browser as you type
215 stars 19 forks source link

Impatient mode doesn't work at all #20

Closed frankjonen closed 4 years ago

frankjonen commented 4 years ago
  1. httpd-start
  2. httpd-serve-directory (if I leave that off a simple reload crashes the server)
  3. impatient-mode
  4. Go to http://localhost:8080/ (serves the correct dir)
  5. Go to http://localhost:8080/imp (after half an hour I gave up waiting)

What steps am I missing?

skeeto commented 4 years ago

I'm not able to reproduce the problem. Following these steps should work fine.

When you say "crash" do you mean Emacs itself actually crashes and stops? What version of Emacs and on what operating system is this? (I know you're using it to avoid a crash, but I want to note that httpd-serve-directory is part of simple-httpd and has nothing to do with impatient-mode itself.)

When you try to view /imp, does anything appear in the httpd buffer? What happens when you try it with "curl -v"?

frankjonen commented 4 years ago

The httpd server crashes i.e. becomes so unresponsive that new connections won't be processed. AKA "stuck". I have to stop it in Emacs and start it again. That works a few times then I have to end Emacs and start it again.

curl -v gets

*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /imp HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> 

I get this in the httpd buffer

(connection "127.0.0.1")
(request
 (date "Wed, 23 Oct 2019 20:40:58 GMT")
 (address "127.0.0.1")
 (get "/imp")
 (headers
  ("GET" "/imp" "HTTP/1.1")
  ("Host" "localhost:8080")
  ("User-Agent" "curl/7.54.0")
  ("Accept" "*/*")
  ("Content" "")))
(redirect "/imp/")
(error 500
       (wrong-type-argument number-or-marker-p
                            [utf-8-unix utf-8-dos utf-8-mac]))
(hard-error
 (wrong-type-argument number-or-marker-p
                      [utf-8-unix utf-8-dos utf-8-mac]))

Note: This is all without httpd-serve-directory. I did it to point and letter of the documentation.

Error 500 to me indicates that something is dearly wrong in the land of imp.

System: macOS Mojave (10.14.6) Emacs: Version 26.3 (9.0) with Spacemacs

anquegi commented 4 years ago

I recently had the same error, httpd-start can serve an index file that I have in httpd-root, but sometimes this also doesn't work and imaptient-mode doesn't work I change debug-on-error to t but nothing happens

This are the traces from httpd buffer


(connection "127.0.0.1")
(request
 (date "sáb, 26 oct 2019 13:34:48 GMT")
 (address "127.0.0.1")
 (get "/imp")
 (headers
  ("GET" "/imp" "HTTP/1.1")
  ("Host" "localhost:9090")
  ("User-Agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0")
  ("Accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  ("Accept-Language" "es,en-US;q=0.7,en;q=0.3")
  ("Accept-Encoding" "gzip, deflate")
  ("Connection" "keep-alive")
  ("Cookie" "_testing_angular_session=C%2FehIWNKuwDi%2BLIF6c%2F%2FlnI%2FXtGVT46pXEZ9F4lV9R9IY%2BWmXzIaByfDeRK%2BTtI3vi6ecTQNvH83VoDrFL00XBOlap7Xn3eC4HuW9rqCN8N43assvOmNotUsSEndQNlhTCGjWm3VFQ4u3jPKrNFtxA8SP9uTzpHS10sbaVN%2FfjJG8hzP%2FvGv%2FOC3lPiaCUzeJTB5MrDyJ83eN7qQbxp4dB4fdBVcpOunLXuJPum6oYbEEdthwU%2FIy2iuyOLaizbQoRJKWl6xIwUGM%2FMxCn9ShvVQ7g30ibBkiZGrI68tEtQ%3D--ieC%2Bg%2Fp9Zyzz%2Fb%2Ft--%2FvJknlsSCeldXlMb%2F4mxQA%3D%3D")
  ("Upgrade-Insecure-Requests" "1")
  ("Content" "")))
(redirect "/imp/")
(error 500
       (wrong-type-argument number-or-marker-p
                            [utf-8-unix utf-8-dos utf-8-mac]))
(hard-error
 (wrong-type-argument number-or-marker-p
                      [utf-8-unix utf-8-dos utf-8-mac]))
(connection "127.0.0.1")
(request
 (date "sáb, 26 oct 2019 13:36:22 GMT")
 (address "127.0.0.1")
 (get "/imp/live/index.html")
 (headers
  ("GET" "/imp/live/index.html" "HTTP/1.1")
  ("Host" "localhost:9090")
  ("User-Agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0")
  ("Accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  ("Accept-Language" "es,en-US;q=0.7,en;q=0.3")
  ("Accept-Encoding" "gzip, deflate")
  ("Connection" "keep-alive")
  ("Cookie" "_testing_angular_session=C%2FehIWNKuwDi%2BLIF6c%2F%2FlnI%2FXtGVT46pXEZ9F4lV9R9IY%2BWmXzIaByfDeRK%2BTtI3vi6ecTQNvH83VoDrFL00XBOlap7Xn3eC4HuW9rqCN8N43assvOmNotUsSEndQNlhTCGjWm3VFQ4u3jPKrNFtxA8SP9uTzpHS10sbaVN%2FfjJG8hzP%2FvGv%2FOC3lPiaCUzeJTB5MrDyJ83eN7qQbxp4dB4fdBVcpOunLXuJPum6oYbEEdthwU%2FIy2iuyOLaizbQoRJKWl6xIwUGM%2FMxCn9ShvVQ7g30ibBkiZGrI68tEtQ%3D--ieC%2Bg%2Fp9Zyzz%2Fb%2Ft--%2FvJknlsSCeldXlMb%2F4mxQA%3D%3D")
  ("Upgrade-Insecure-Requests" "1")
  ("Content" "")))
(redirect "/imp/live/index.html/")
(error 500
       (wrong-type-argument number-or-marker-p
                            [utf-8-unix utf-8-dos utf-8-mac]))
(hard-error
 (wrong-type-argument number-or-marker-p
                      [utf-8-unix utf-8-dos utf-8-mac]))
(connection "127.0.0.1")
(request
 (date "sáb, 26 oct 2019 13:47:11 GMT")
 (address "127.0.0.1")
 (get "/")
 (headers
  ("GET" "/" "HTTP/1.1")
  ("Host" "localhost:9090")
  ("User-Agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0")
  ("Accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  ("Accept-Language" "es,en-US;q=0.7,en;q=0.3")
  ("Accept-Encoding" "gzip, deflate")
  ("Connection" "keep-alive")
  ("Cookie" "_testing_angular_session=C%2FehIWNKuwDi%2BLIF6c%2F%2FlnI%2FXtGVT46pXEZ9F4lV9R9IY%2BWmXzIaByfDeRK%2BTtI3vi6ecTQNvH83VoDrFL00XBOlap7Xn3eC4HuW9rqCN8N43assvOmNotUsSEndQNlhTCGjWm3VFQ4u3jPKrNFtxA8SP9uTzpHS10sbaVN%2FfjJG8hzP%2FvGv%2FOC3lPiaCUzeJTB5MrDyJ83eN7qQbxp4dB4fdBVcpOunLXuJPum6oYbEEdthwU%2FIy2iuyOLaizbQoRJKWl6xIwUGM%2FMxCn9ShvVQ7g30ibBkiZGrI68tEtQ%3D--ieC%2Bg%2Fp9Zyzz%2Fb%2Ft--%2FvJknlsSCeldXlMb%2F4mxQA%3D%3D")
  ("Upgrade-Insecure-Requests" "1")
  ("If-Modified-Since" "sáb, 26 oct 2019 13:31:23 GMT")
  ("If-None-Match" "\"77cd7ecb53300fc7\"")
  ("Content" "")))
(file "/Users/toni/public_html/index.html" not-modified)
(error 500
       (wrong-type-argument number-or-marker-p
                            [utf-8-unix utf-8-dos utf-8-mac]))
(hard-error
 (wrong-type-argument number-or-marker-p
                      [utf-8-unix utf-8-dos utf-8-mac]))
(stop "Sat Oct 26 15:47:39 2019")
(start "Sat Oct 26 15:47:47 2019")
(connection "127.0.0.1")
(request
 (date "sáb, 26 oct 2019 13:47:50 GMT")
 (address "127.0.0.1")
 (get "/")
 (headers
  ("GET" "/" "HTTP/1.1")
  ("Host" "localhost:9090")
  ("User-Agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0")
  ("Accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  ("Accept-Language" "es,en-US;q=0.7,en;q=0.3")
  ("Accept-Encoding" "gzip, deflate")
  ("Connection" "keep-alive")
  ("Cookie" "_testing_angular_session=C%2FehIWNKuwDi%2BLIF6c%2F%2FlnI%2FXtGVT46pXEZ9F4lV9R9IY%2BWmXzIaByfDeRK%2BTtI3vi6ecTQNvH83VoDrFL00XBOlap7Xn3eC4HuW9rqCN8N43assvOmNotUsSEndQNlhTCGjWm3VFQ4u3jPKrNFtxA8SP9uTzpHS10sbaVN%2FfjJG8hzP%2FvGv%2FOC3lPiaCUzeJTB5MrDyJ83eN7qQbxp4dB4fdBVcpOunLXuJPum6oYbEEdthwU%2FIy2iuyOLaizbQoRJKWl6xIwUGM%2FMxCn9ShvVQ7g30ibBkiZGrI68tEtQ%3D--ieC%2Bg%2Fp9Zyzz%2Fb%2Ft--%2FvJknlsSCeldXlMb%2F4mxQA%3D%3D")
  ("Upgrade-Insecure-Requests" "1")
  ("If-Modified-Since" "sáb, 26 oct 2019 13:31:23 GMT")
  ("If-None-Match" "\"77cd7ecb53300fc7\"")
  ("Content" "")))
(file "/Users/toni/public_html/index.html" not-modified)
(error 500
       (wrong-type-argument number-or-marker-p
                            [utf-8-unix utf-8-dos utf-8-mac]))
(hard-error
 (wrong-type-argument number-or-marker-p
                      [utf-8-unix utf-8-dos utf-8-mac]))

I'm also using mac os x

GNU Emacs 27.0.50 (build 1, x86_64-apple-darwin19.0.0, NS appkit-1894.00 Version 10.15 (Build 19A583)) of 2019-10-09

and mac os catalina.

I'm not sure how to debug. this but instrumenting with edebug httpd-send-file, I get this:

Result: nil
 [4 times]
Result: (("GET" #("/" 0 1 (charset iso-8859-1)) "HTTP/1.1") ("Host" #("localhost:9090" 0 14 (charset iso-8859-1))) ("User-Agent" #("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:7..." 0 82 (charset iso-8859-1))) ("Accept" #("text/html,application/xhtml+xml,application/xml;q=..." 0 63 (charset iso-8859-1))) ("Accept-Language" #("es,en-US;q=0.7,en;q=0.3" 0 23 (charset iso-8859-1))) ("Accept-Encoding" #("gzip, deflate" 0 13 (charset iso-8859-1))) ("Connection" #("keep-alive" 0 10 (charset iso-8859-1))) ("Cookie" #("_testing_angular_session=C%2FehIWNKuwDi%2BLIF6c%2F..." 0 411 (charset iso-8859-1))) ("Upgrade-Insecure-Requests" #("1" 0 1 (charset iso-8859-1))) ("If-Modified-Since" #("sáb, 26 oct 2019 13:31:23 GMT" 0 30 (charset iso-8859-1))) ("If-None-Match" #("\"77cd7ecb53300fc7\"" 0 18 (charset iso-8859-1))) ("Content" ""))

Result: ("If-None-Match" #("\"77cd7ecb53300fc7\"" 0 18 (charset iso-8859-1)))

Result: #("\"77cd7ecb53300fc7\"" 0 18 (charset iso-8859-1))
 [2 times]
Result: "/Users/toni/public_html/index.html"

Result: "\"77cd7ecb53300fc7\""
 [4 times]
Result: "/Users/toni/public_html/index.html"

Result: (nil 1 501 20 (23988 19115 716905 447000) (23988 19114 524451 718000) (23988 19114 524451 718000) 136 "-rw-r--r--" t 8645966726 16777220)

Result: (23988 19115 716905 447000)

Result: "sáb, 26 oct 2019 13:31:23 GMT"
 [3 times]
Result: #("\"77cd7ecb53300fc7\"" 0 18 (charset iso-8859-1))

Result: "\"77cd7ecb53300fc7\""

Result: t
 [4 times]
Result: "/Users/toni/public_html/index.html"

Result: (file "/Users/toni/public_html/index.html" not-modified)

Result: nil
 [2 times]
Result: #<process httpd <127.0.0.1:50192>>
 [2 times]
Result: nil
 [3 times]
Result: 500 (#o764, #x1f4, ?Ǵ)

Result: (wrong-type-argument number-or-marker-p [utf-8-unix utf-8-dos utf-8-mac])

Result: (error 500 (wrong-type-argument number-or-marker-p [utf-8-unix utf-8-dos utf-8-mac]))

Result: nil
 [6 times]
Result: 500 (#o764, #x1f4, ?Ǵ)

Result: ((403 . "<!DOCTYPE html>\n<html><head>\n<title>403 Forbidden<...") (404 . "<!DOCTYPE html>\n<html><head>\n<title>404 Not Found<...") (500 . "<!DOCTYPE html>\n<html><head>\n<title>500 Internal E..."))

Result: (500 . "<!DOCTYPE html>\n<html><head>\n<title>500 Internal E...")

Result: "<!DOCTYPE html>\n<html><head>\n<title>500 Internal E..."

Result: "<!DOCTYPE html>\n<html><head>\n<title>500 Internal E..."
 [3 times]
Result: (wrong-type-argument number-or-marker-p [utf-8-unix utf-8-dos utf-8-mac])

Result: nil
 [4 times]
Result: #<buffer  *temp*-505204>
 [2 times]
Result: nil
 [2 times]
Result: (wrong-type-argument number-or-marker-p [utf-8-unix utf-8-dos utf-8-mac])

Result: (wrong-type-argument number-or-marker-p [utf-8-unix utf-8-dos utf-8-mac])
 [2 times]
Result: nil
 [2 times]
Result: nil

Result: nil
 [2 times]
Result: nil
 [2 times]
Result: "error: (wrong-type-argument number-or-marker-p [ut..."

Result: "error: (wrong-type-argument number-or-marker-p [ut..."

Result: "error: (wrong-type-argument number-or-marker-p [ut..."

Result: "error: (wrong-type-argument number-or-marker-p [ut..."
 [3 times]
Result: "<!DOCTYPE html>\n<html><head>\n<title>500 Internal E..."

Result: "error: (wrong-type-argument number-or-marker-p [ut..."

Result: "<!DOCTYPE html>\n<html><head>\n<title>500 Internal E..."

Result: nil

Result: nil
 [2 times]
Result: #<process httpd <127.0.0.1:50192>>

Result: 500 (#o764, #x1f4, ?Ǵ)

I cannot get anything more usefull, this is edebug-trace

{ httpd-send-file args: (httpd <127.0.0.1:50407> /Users/toni/public_html/index.html ((GET / HTTP/1.1) (Host localhost:9090) (User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0) (Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8) (Accept-Language es,en-US;q=0.7,en;q=0.3) (Accept-Encoding gzip, deflate) (Connection keep-alive) (Cookie _testing_angular_session=C%2FehIWNKuwDi%2BLIF6c%2F%2FlnI%2FXtGVT46pXEZ9F4lV9R9IY%2BWmXzIaByfDeRK%2BTtI3vi6ecTQNvH83VoDrFL00XBOlap7Xn3eC4HuW9rqCN8N43assvOmNotUsSEndQNlhTCGjWm3VFQ4u3jPKrNFtxA8SP9uTzpHS10sbaVN%2FfjJG8hzP%2FvGv%2FOC3lPiaCUzeJTB5MrDyJ83eN7qQbxp4dB4fdBVcpOunLXuJPum6oYbEEdthwU%2FIy2iuyOLaizbQoRJKWl6xIwUGM%2FMxCn9ShvVQ7g30ibBkiZGrI68tEtQ%3D--ieC%2Bg%2Fp9Zyzz%2Fb%2Ft--%2FvJknlsSCeldXlMb%2F4mxQA%3D%3D) (Upgrade-Insecure-Requests 1) (If-Modified-Since sáb, 26 oct 2019 13:31:23 GMT) (If-None-Match "77cd7ecb53300fc7") (Content )))
{ httpd-error args: (httpd <127.0.0.1:50407> 500 (wrong-type-argument number-or-marker-p [utf-8-unix utf-8-dos utf-8-mac]))
anquegi commented 4 years ago

I'm not sure about coding systems, but in my case the problem comes from this function:

(defun httpd--buffer-size (&optional buffer)
  "Get the buffer size in bytes."
  (bufferpos-to-filepos (point-max)))

i.e. when calculating the buffer-size in bytes, the problem was that in find utf-8 instead of utf-8-dos, utf-8-mac, utf-8-unix, and with the function:

(defun bufferpos-to-filepos (position &optional quality coding-system)
  "Try to return the file byte corresponding to a particular buffer POSITION.
Value is the file position given as a (0-based) byte count.
The function presumes the file is encoded with CODING-SYSTEM, which defaults
to `buffer-file-coding-system'.
QUALITY can be:
  `approximate', in which case we may cut some corners to avoid
    excessive work.
  `exact', in which case we may end up re-(en/de)coding a large
    part of the file/buffer, this can be expensive and slow.
  nil, in which case we may return nil rather than an approximation."
  (unless coding-system (setq coding-system buffer-file-coding-system))
  (let* ((eol (coding-system-eol-type coding-system))
         (lineno (if (= eol 1) (1- (line-number-at-pos position)) 0))
         (type (coding-system-type coding-system))
         (base (coding-system-base coding-system))
         (point-min 1))                 ;Clarify what the `1' means.
    (and (eq type 'utf-8)
         ;; Any post-read/pre-write conversions mean it's not really UTF-8.
         (not (null (coding-system-get coding-system :post-read-conversion)))
         (setq type 'not-utf-8))
    (and (memq type '(charset raw-text undecided))
         ;; The following are all of type 'charset', but they are
         ;; actually variable-width encodings.
         (not (memq base '(chinese-gbk chinese-gb18030 euc-tw euc-jis-2004
                                       korean-iso-8bit chinese-iso-8bit
                                       japanese-iso-8bit chinese-big5-hkscs
                                       japanese-cp932 korean-cp949)))
         (setq type 'single-byte))
    (pcase type
      ('utf-8
       (+ (or (position-bytes position)
              (if (<= position 0)
                  point-min
                (position-bytes (point-max))))
          ;; Account for BOM, if any.
          (if (coding-system-get coding-system :bom) 3 0)
          ;; Account for CR in CRLF pairs.
          lineno
          (- point-min)))
      ('single-byte
       (+ position (- point-min) lineno))
      ((and 'utf-16
            ;; FIXME: For utf-16, we could use the same approach as used for
            ;; dos EOLs (counting the number of non-BMP chars instead of the
            ;; number of lines).
            (guard (not (eq quality 'exact))))
       ;; In approximate mode, assume all characters are within the
       ;; BMP, i.e. each one takes up 2 bytes.
       (+ (* (- position point-min) 2)
          ;; Account for BOM, if any.
          (if (coding-system-get coding-system :bom) 2 0)
          ;; Account for CR in CRLF pairs.
          lineno))
      (_
       (pcase quality
         ('approximate (+ (position-bytes position) (- point-min) lineno))
         ('exact
          ;; Rather than assume that the file exists and still holds the right
          ;; data, we reconstruct its relevant portion.
          (let ((buf (current-buffer)))
            (with-temp-buffer
              (set-buffer-multibyte nil)
              (let ((tmp-buf (current-buffer)))
                (with-current-buffer buf
                  (save-restriction
                    (widen)
                    (encode-coding-region (point-min) (min (point-max) position)
                                          coding-system tmp-buf)))
                (buffer-size))))))))))

Concretely it fails at this line: lineno (if (= eol 1) (1- (line-number-at-pos position)) 0))

Where in the coding-system is an array a not a number, because the variable, buffer-file-coding-system is 'utf-8 instead of one of this 'utf-8-mac, 'utf-8-unix, 'utf-8-dos, so it works as follow:

ELISP> buffer-file-coding-system
utf-8-mac
ELISP> (prefer-coding-system 'utf-8)
nil
ELISP> buffer-file-coding-system
utf-8
ELISP> (coding-system-eol-type 'utf-8)
[utf-8-unix utf-8-dos utf-8-mac]

ELISP> (coding-system-eol-type 'utf-8-mac)
2 (#o2, #x2, ?\C-b)

To solve this incovenience, in my init-file I change: (prefer-coding-system 'utf-8) to (prefer-coding-system 'utf-8-mac)

since I'm in a mac, and it fixed the problem, so maybe this can fix your problem, I think that you are also on a mac. So really the problem is how this function detects the file coding-system and in simple-httpd package, not in impatiente-mode

frankjonen commented 4 years ago

Yep! (prefer-coding-system 'utf-8-mac) did the trick! Should be moved to a FAQ. I'm using it with Helium browser for a persistent "panel" closer to the dying Espresso app. Flickers a lot and is finicky about paths. For adjusting on-the-fly though it works well. Haven't tried CSS yet. Adding IMP to the CSS file gets me a fancy preview of the CSS code though. Helpful for code presentations as Safari copies that as Rich Text.

I'm glad this could be fixed so elegantly. It seems to relate to other errors as well. My Emacs startup time is snappier now (find file is now slower though in reacting).

mphojele commented 4 years ago

I think this issue should be closed now, with the solution being (prefer-coding-system 'utf-8-[os])

skeeto commented 4 years ago

Alright, it looks like this broke when I made the change 67f2f1e in simple-httpd. That commit used bufferpos-to-filepos, which involves the buffer's file coding system. However, these buffers are not written to a file but to the network, then discarded. I reverted that change, and hopefully it's fixed by 22ce66e.

The hang was because the reported size (content-length) was based on the bytes to be written to a file. This didn't match the actual size sent over the network, so clients were waiting for those last bytes that would never arrive.