import time
from mitmproxy import exceptions

BLOCKSIZE = 1024
# It's not clear what the upper limit for time.sleep is. It's lower than the
# maximum int or float. 1 year should do.
FOREVER = 60 * 60 * 24 * 365


def send_chunk(fp, val, blocksize, start, end):
    """
        (start, end): Inclusive lower bound, exclusive upper bound.
    """
    for i in range(start, end, blocksize):
        fp.write(
            val[i:min(i + blocksize, end)]
        )
    return end - start


def write_values(fp, vals, actions, sofar=0, blocksize=BLOCKSIZE):
    """
        vals: A list of values, which may be strings or Value objects.

        actions: A list of (offset, action, arg) tuples. Action may be "inject",
        "pause" or "disconnect".

        Both vals and actions are in reverse order, with the first items last.

        Return True if connection should disconnect.
    """
    sofar = 0
    try:
        while vals:
            v = vals.pop()
            offset = 0
            while actions and actions[-1][0] < (sofar + len(v)):
                a = actions.pop()
                offset += send_chunk(
                    fp,
                    v,
                    blocksize,
                    offset,
                    a[0] - sofar - offset
                )
                if a[1] == "pause":
                    time.sleep(
                        FOREVER if a[2] == "f" else a[2]
                    )
                elif a[1] == "disconnect":
                    return True
                elif a[1] == "inject":
                    send_chunk(fp, a[2], blocksize, 0, len(a[2]))
            send_chunk(fp, v, blocksize, offset, len(v))
            sofar += len(v)
        # Remainders
        while actions:
            a = actions.pop()
            if a[1] == "pause":
                time.sleep(a[2])
            elif a[1] == "disconnect":
                return True
            elif a[1] == "inject":
                send_chunk(fp, a[2], blocksize, 0, len(a[2]))
    except exceptions.TcpDisconnect:  # pragma: no cover
        return True

pathod/language/writer.py
import time
from mitmproxy import exceptions

BLOCKSIZE = 1024
# It's not clear what the upper limit for time.sleep is. It's lower than the
# maximum int or float. 1 year should do.
FOREVER = 60 * 60 * 24 * 365


def send_chunk(fp, val, blocksize, start, end):
    """
        (start, end): Inclusive lower bound, exclusive upper bound.
    """
    for i in range(start, end, blocksize):
        fp.write(
            val[i:min(i + blocksize, end)]
        )
    return end - start


def write_values(fp, vals, actions, sofar=0, blocksize=BLOCKSIZE):
    """
        vals: A list of values, which may be strings or Value objects.

        actions: A list of (offset, action, arg) tuples. Action may be "inject",
        "pause" or "disconnect".

        Both vals and actions are in reverse order, with the first items last.

        Return True if connection should disconnect.
    """
    sofar = 0
    try:
        while vals:
            v = vals.pop()
            offset = 0
            while actions and actions[-1][0] < (sofar + len(v)):
                a = actions.pop()
                offset += send_chunk(
                    fp,
                    v,
                    blocksize,
                    offset,
                    a[0] - sofar - offset
                )
                if a[1] == "pause":
                    time.sleep(
                        FOREVER if a[2] == "f" else a[2]
                    )
                elif a[1] == "disconnect":
                    return True
                elif a[1] == "inject":
                    send_chunk(fp, a[2], blocksize, 0, len(a[2]))
            send_chunk(fp, v, blocksize, offset, len(v))
            sofar += len(v)
        # Remainders
        while actions:
            a = actions.pop()
            if a[1] == "pause":
                time.sleep(
                    FOREVER if a[2] == "f" else a[2]
                )
            elif a[1] == "disconnect":
                return True
            elif a[1] == "inject":
                send_chunk(fp, a[2], blocksize, 0, len(a[2]))
    except exceptions.TcpDisconnect:  # pragma: no cover
        return True

pathod/language/writer.py
diff --git a/pathod/language/writer.py b/pathod/language/writer.py
index ac0f44dac9..cdb62c3a6c 100644
--- a/pathod/language/writer.py
+++ b/pathod/language/writer.py
@@ -57,7 +57,9 @@ def write_values(fp, vals, actions, sofar=0, blocksize=BLOCKSIZE):
         while actions:
             a = actions.pop()
             if a[1] == "pause":
-                time.sleep(a[2])
+                time.sleep(
+                    FOREVER if a[2] == "f" else a[2]
+                )
             elif a[1] == "disconnect":
                 return True
             elif a[1] == "inject":

pathoc does not accept `:pa,f` to pause forever at end of message
##### Steps to reproduce the problem:

`pathoc www.example.com 'get:/:pa,f'`
##### What is the expected behavior?

Send request, but pause forever after sending.
##### What went wrong?

I get a stack trace with "a float is required".

```
$ pathoc www.example.com 'get:/:pa,f'
08-09-16 16:59:41: >> 'GET':/:pa,f
Traceback (most recent call last):
  File "/usr/local/bin/pathoc", line 11, in <module>
    sys.exit(go_pathoc())
  File "/usr/local/lib/python2.7/dist-packages/pathod/pathoc_cmdline.py", line 226, in go_pathoc
    pathoc.main(args)
  File "/usr/local/lib/python2.7/dist-packages/pathod/pathoc.py", line 522, in main
    ret = p.request(spec)
  File "/usr/local/lib/python2.7/dist-packages/pathod/pathoc.py", line 452, in request
    return self.http(r)
  File "/usr/local/lib/python2.7/dist-packages/pathod/pathoc.py", line 432, in http
    return resp
  File "/usr/local/lib/python2.7/dist-packages/pathod/pathoc.py", line 411, in http
    req = language.serve(r, self.wfile, self.settings)
  File "/usr/local/lib/python2.7/dist-packages/pathod/language/__init__.py", line 105, in serve
    disconnect = writer.write_values(fp, vals, actions[:])
  File "/usr/local/lib/python2.7/dist-packages/pathod/language/writer.py", line 61, in write_values
    time.sleep(a[2])
TypeError: a float is required
```
##### Any other comments? What have you tried so far?

All other combinations of pause flags work as expected:

```
$ pathoc www.example.com 'get:/:p2,5'
08-09-16 17:05:07: >> 'GET':/:p2,5
<< 200 OK: 1270 bytes
$ pathoc www.example.com 'get:/:pr,5'
08-09-16 17:05:21: >> 'GET':/:pr,5
<< 200 OK: 1270 bytes
$ pathoc www.example.com 'get:/:pa,5'
08-09-16 17:05:41: >> 'GET':/:pa,5
<< 200 OK: 1270 bytes
$ pathoc www.example.com 'get:/:p2,f'
^C08-09-16 17:04:46: >> 'GET':/:p2,f
$ pathoc www.example.com 'get:/:pr,f'
^C08-09-16 17:04:55: >> 'GET':/:pr,f
```

---

pathoc version: 0.17
Operating System: Debian Linux 8.5 "Jessie" x64

