Merge pull request #10248 from narimiran/graveyard
Move four modules to graveyard
This commit is contained in:
commit
6389271d1c
10 changed files with 27 additions and 1073 deletions
23
changelog.md
23
changelog.md
|
|
@ -1,4 +1,5 @@
|
||||||
## v0.20.0 - XX/XX/2018
|
## v0.20.0 - XX/XX/2019
|
||||||
|
|
||||||
|
|
||||||
### Changes affecting backwards compatibility
|
### Changes affecting backwards compatibility
|
||||||
|
|
||||||
|
|
@ -26,6 +27,7 @@
|
||||||
- `getImpl` on a `var` or `let` symbol will now return the full `IdentDefs`
|
- `getImpl` on a `var` or `let` symbol will now return the full `IdentDefs`
|
||||||
tree from the symbol declaration instead of just the initializer portion.
|
tree from the symbol declaration instead of just the initializer portion.
|
||||||
|
|
||||||
|
|
||||||
#### Breaking changes in the standard library
|
#### Breaking changes in the standard library
|
||||||
|
|
||||||
- `osproc.execProcess` now also takes a `workingDir` parameter.
|
- `osproc.execProcess` now also takes a `workingDir` parameter.
|
||||||
|
|
@ -57,6 +59,13 @@
|
||||||
|
|
||||||
- `isNil` is no longer false for undefined in the JavaScript backend: now it's true for both nil and undefined. Use `isNull` or `isUndefined` if you need exact equallity: `isNil` is consistent with `===`, `isNull` and `isUndefined` with `==`.
|
- `isNil` is no longer false for undefined in the JavaScript backend: now it's true for both nil and undefined. Use `isNull` or `isUndefined` if you need exact equallity: `isNil` is consistent with `===`, `isNull` and `isUndefined` with `==`.
|
||||||
|
|
||||||
|
- several deprecated modules were removed: `ssl`, `matchers`, `httpserver`,
|
||||||
|
`unsigned`, `actors`, `parseurl`
|
||||||
|
|
||||||
|
- several poorly documented and not used modules were moved to graveyard
|
||||||
|
(they are available as Nimble packages): `subexes`, `scgi`, `smtp`, `oids`
|
||||||
|
|
||||||
|
|
||||||
#### Breaking changes in the compiler
|
#### Breaking changes in the compiler
|
||||||
|
|
||||||
- The compiler now implements the "generic symbol prepass" for `when` statements
|
- The compiler now implements the "generic symbol prepass" for `when` statements
|
||||||
|
|
@ -97,13 +106,16 @@ proc enumToString*(enums: openArray[enum]): string =
|
||||||
is instantiation of generic proc symbol.
|
is instantiation of generic proc symbol.
|
||||||
|
|
||||||
- Added the parameter ``isSorted`` for the ``sequtils.deduplicate`` proc.
|
- Added the parameter ``isSorted`` for the ``sequtils.deduplicate`` proc.
|
||||||
|
|
||||||
- There is a new stdlib module `std/diff` to compute the famous "diff"
|
- There is a new stdlib module `std/diff` to compute the famous "diff"
|
||||||
of two texts by line.
|
of two texts by line.
|
||||||
|
|
||||||
- Added `os.relativePath`.
|
- Added `os.relativePath`.
|
||||||
|
|
||||||
- Added `parseopt.remainingArgs`.
|
- Added `parseopt.remainingArgs`.
|
||||||
|
|
||||||
- Added `os.getCurrentCompilerExe` (implmented as `getAppFilename` at CT),
|
- Added `os.getCurrentCompilerExe` (implmented as `getAppFilename` at CT),
|
||||||
can be used to retrive the currently executing compiler.
|
can be used to retrieve the currently executing compiler.
|
||||||
|
|
||||||
|
|
||||||
### Library changes
|
### Library changes
|
||||||
|
|
@ -128,10 +140,12 @@ proc enumToString*(enums: openArray[enum]): string =
|
||||||
- `os.joinPath` and `os.normalizePath` handle edge cases like ``"a/b/../../.."``
|
- `os.joinPath` and `os.normalizePath` handle edge cases like ``"a/b/../../.."``
|
||||||
differently.
|
differently.
|
||||||
|
|
||||||
|
- `securehash` is moved to `lib/deprecated`
|
||||||
|
|
||||||
|
|
||||||
### Language additions
|
### Language additions
|
||||||
|
|
||||||
- Vm suport for float32<->int32 and float64<->int64 casts was added.
|
- Vm support for float32<->int32 and float64<->int64 casts was added.
|
||||||
- There is a new pragma block `noSideEffect` that works like
|
- There is a new pragma block `noSideEffect` that works like
|
||||||
the `gcsafe` pragma block.
|
the `gcsafe` pragma block.
|
||||||
- added os.getCurrentProcessId()
|
- added os.getCurrentProcessId()
|
||||||
|
|
@ -140,6 +154,7 @@ proc enumToString*(enums: openArray[enum]): string =
|
||||||
pragmas for further analysis by macros
|
pragmas for further analysis by macros
|
||||||
- Custom pragmas are now supported for `var` and `let` symbols.
|
- Custom pragmas are now supported for `var` and `let` symbols.
|
||||||
|
|
||||||
|
|
||||||
### Language changes
|
### Language changes
|
||||||
|
|
||||||
- The standard extension for SCF (source code filters) files was changed from
|
- The standard extension for SCF (source code filters) files was changed from
|
||||||
|
|
@ -161,7 +176,9 @@ proc enumToString*(enums: openArray[enum]): string =
|
||||||
- `nimpretty`: --backup now defaults to `off` instead of `on` and the flag was
|
- `nimpretty`: --backup now defaults to `off` instead of `on` and the flag was
|
||||||
un-documented; use `git` instead of relying on backup files.
|
un-documented; use `git` instead of relying on backup files.
|
||||||
|
|
||||||
|
|
||||||
### Compiler changes
|
### Compiler changes
|
||||||
- The deprecated `fmod` proc is now unavailable on the VM'.
|
- The deprecated `fmod` proc is now unavailable on the VM'.
|
||||||
|
|
||||||
|
|
||||||
### Bugfixes
|
### Bugfixes
|
||||||
|
|
|
||||||
15
doc/lib.rst
15
doc/lib.rst
|
|
@ -141,9 +141,6 @@ String handling
|
||||||
Ropes can represent very long strings efficiently; especially concatenation
|
Ropes can represent very long strings efficiently; especially concatenation
|
||||||
is done in O(1) instead of O(n).
|
is done in O(1) instead of O(n).
|
||||||
|
|
||||||
* `subexes <subexes.html>`_
|
|
||||||
This module implements advanced string substitution operations.
|
|
||||||
|
|
||||||
* `std/editdistance <editdistance.html>`_
|
* `std/editdistance <editdistance.html>`_
|
||||||
This module contains an algorithm to compute the edit distance between two
|
This module contains an algorithm to compute the edit distance between two
|
||||||
Unicode strings.
|
Unicode strings.
|
||||||
|
|
@ -233,9 +230,6 @@ Internet Protocols and Support
|
||||||
* `cgi <cgi.html>`_
|
* `cgi <cgi.html>`_
|
||||||
This module implements helpers for CGI applications.
|
This module implements helpers for CGI applications.
|
||||||
|
|
||||||
* `scgi <scgi.html>`_
|
|
||||||
This module implements helpers for SCGI applications.
|
|
||||||
|
|
||||||
* `browsers <browsers.html>`_
|
* `browsers <browsers.html>`_
|
||||||
This module implements procs for opening URLs with the user's default
|
This module implements procs for opening URLs with the user's default
|
||||||
browser.
|
browser.
|
||||||
|
|
@ -244,9 +238,6 @@ Internet Protocols and Support
|
||||||
This module implements a simple HTTP client which supports both synchronous
|
This module implements a simple HTTP client which supports both synchronous
|
||||||
and asynchronous retrieval of web pages.
|
and asynchronous retrieval of web pages.
|
||||||
|
|
||||||
* `smtp <smtp.html>`_
|
|
||||||
This module implement a simple SMTP client.
|
|
||||||
|
|
||||||
* `cookies <cookies.html>`_
|
* `cookies <cookies.html>`_
|
||||||
This module contains helper procs for parsing and generating cookies.
|
This module contains helper procs for parsing and generating cookies.
|
||||||
|
|
||||||
|
|
@ -381,12 +372,6 @@ Multimedia support
|
||||||
Miscellaneous
|
Miscellaneous
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
* `oids <oids.html>`_
|
|
||||||
An OID is a global ID that consists of a timestamp,
|
|
||||||
a unique counter and a random value. This combination should suffice to
|
|
||||||
produce a globally distributed unique ID. This implementation was extracted
|
|
||||||
from the Mongodb interface and it thus binary compatible with a Mongo OID.
|
|
||||||
|
|
||||||
* `endians <endians.html>`_
|
* `endians <endians.html>`_
|
||||||
This module contains helpers that deal with different byte orders.
|
This module contains helpers that deal with different byte orders.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,93 +0,0 @@
|
||||||
#
|
|
||||||
#
|
|
||||||
# Nim's Runtime Library
|
|
||||||
# (c) Copyright 2013 Andreas Rumpf
|
|
||||||
#
|
|
||||||
# See the file "copying.txt", included in this
|
|
||||||
# distribution, for details about the copyright.
|
|
||||||
#
|
|
||||||
|
|
||||||
## Nim OID support. An OID is a global ID that consists of a timestamp,
|
|
||||||
## a unique counter and a random value. This combination should suffice to
|
|
||||||
## produce a globally distributed unique ID. This implementation was extracted
|
|
||||||
## from the Mongodb interface and it thus binary compatible with a Mongo OID.
|
|
||||||
##
|
|
||||||
## This implementation calls ``math.randomize()`` for the first call of
|
|
||||||
## ``genOid``.
|
|
||||||
|
|
||||||
import times, endians
|
|
||||||
|
|
||||||
type
|
|
||||||
Oid* = object ## an OID
|
|
||||||
time: int32 ##
|
|
||||||
fuzz: int32 ##
|
|
||||||
count: int32 ##
|
|
||||||
|
|
||||||
proc `==`*(oid1: Oid, oid2: Oid): bool =
|
|
||||||
## Compare two Mongo Object IDs for equality
|
|
||||||
return (oid1.time == oid2.time) and (oid1.fuzz == oid2.fuzz) and (oid1.count == oid2.count)
|
|
||||||
|
|
||||||
proc hexbyte*(hex: char): int =
|
|
||||||
case hex
|
|
||||||
of '0'..'9': result = (ord(hex) - ord('0'))
|
|
||||||
of 'a'..'f': result = (ord(hex) - ord('a') + 10)
|
|
||||||
of 'A'..'F': result = (ord(hex) - ord('A') + 10)
|
|
||||||
else: discard
|
|
||||||
|
|
||||||
proc parseOid*(str: cstring): Oid =
|
|
||||||
## parses an OID.
|
|
||||||
var bytes = cast[cstring](addr(result.time))
|
|
||||||
var i = 0
|
|
||||||
while i < 12:
|
|
||||||
bytes[i] = chr((hexbyte(str[2 * i]) shl 4) or hexbyte(str[2 * i + 1]))
|
|
||||||
inc(i)
|
|
||||||
|
|
||||||
proc oidToString*(oid: Oid, str: cstring) =
|
|
||||||
const hex = "0123456789abcdef"
|
|
||||||
# work around a compiler bug:
|
|
||||||
var str = str
|
|
||||||
var o = oid
|
|
||||||
var bytes = cast[cstring](addr(o))
|
|
||||||
var i = 0
|
|
||||||
while i < 12:
|
|
||||||
let b = bytes[i].ord
|
|
||||||
str[2 * i] = hex[(b and 0xF0) shr 4]
|
|
||||||
str[2 * i + 1] = hex[b and 0xF]
|
|
||||||
inc(i)
|
|
||||||
str[24] = '\0'
|
|
||||||
|
|
||||||
proc `$`*(oid: Oid): string =
|
|
||||||
result = newString(24)
|
|
||||||
oidToString(oid, result)
|
|
||||||
|
|
||||||
var
|
|
||||||
incr: int
|
|
||||||
fuzz: int32
|
|
||||||
|
|
||||||
proc genOid*(): Oid =
|
|
||||||
## generates a new OID.
|
|
||||||
proc rand(): cint {.importc: "rand", header: "<stdlib.h>", nodecl.}
|
|
||||||
proc srand(seed: cint) {.importc: "srand", header: "<stdlib.h>", nodecl.}
|
|
||||||
|
|
||||||
var t = getTime().toUnix.int32
|
|
||||||
|
|
||||||
var i = int32(atomicInc(incr))
|
|
||||||
|
|
||||||
if fuzz == 0:
|
|
||||||
# racy, but fine semantically:
|
|
||||||
srand(t)
|
|
||||||
fuzz = rand()
|
|
||||||
bigEndian32(addr result.time, addr(t))
|
|
||||||
result.fuzz = fuzz
|
|
||||||
bigEndian32(addr result.count, addr(i))
|
|
||||||
|
|
||||||
proc generatedTime*(oid: Oid): Time =
|
|
||||||
## returns the generated timestamp of the OID.
|
|
||||||
var tmp: int32
|
|
||||||
var dummy = oid.time
|
|
||||||
bigEndian32(addr(tmp), addr(dummy))
|
|
||||||
result = fromUnix(tmp)
|
|
||||||
|
|
||||||
when not defined(testing) and isMainModule:
|
|
||||||
let xo = genOid()
|
|
||||||
echo xo.generatedTime
|
|
||||||
|
|
@ -1,295 +0,0 @@
|
||||||
#
|
|
||||||
#
|
|
||||||
# Nim's Runtime Library
|
|
||||||
# (c) Copyright 2013 Andreas Rumpf, Dominik Picheta
|
|
||||||
#
|
|
||||||
# See the file "copying.txt", included in this
|
|
||||||
# distribution, for details about the copyright.
|
|
||||||
#
|
|
||||||
|
|
||||||
## This module implements helper procs for SCGI applications. Example:
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
##
|
|
||||||
## import strtabs, sockets, scgi
|
|
||||||
##
|
|
||||||
## var counter = 0
|
|
||||||
## proc handleRequest(client: Socket, input: string,
|
|
||||||
## headers: StringTableRef): bool {.procvar.} =
|
|
||||||
## inc(counter)
|
|
||||||
## client.writeStatusOkTextContent()
|
|
||||||
## client.send("Hello for the $#th time." % $counter & "\c\L")
|
|
||||||
## return false # do not stop processing
|
|
||||||
##
|
|
||||||
## run(handleRequest)
|
|
||||||
##
|
|
||||||
## **Warning:** The API of this module is unstable, and therefore is subject
|
|
||||||
## to change.
|
|
||||||
##
|
|
||||||
## **Warning:** This module only supports the old asynchronous interface.
|
|
||||||
## You may wish to use the `asynchttpserver <asynchttpserver.html>`_
|
|
||||||
## instead for web applications.
|
|
||||||
|
|
||||||
include "system/inclrtl"
|
|
||||||
|
|
||||||
import sockets, strutils, os, strtabs, asyncio
|
|
||||||
|
|
||||||
type
|
|
||||||
ScgiError* = object of IOError ## the exception that is raised, if a SCGI error occurs
|
|
||||||
|
|
||||||
proc raiseScgiError*(msg: string) {.noreturn.} =
|
|
||||||
## raises an ScgiError exception with message `msg`.
|
|
||||||
var e: ref ScgiError
|
|
||||||
new(e)
|
|
||||||
e.msg = msg
|
|
||||||
raise e
|
|
||||||
|
|
||||||
proc parseWord(inp: string, outp: var string, start: int): int =
|
|
||||||
result = start
|
|
||||||
while inp[result] != '\0': inc(result)
|
|
||||||
outp = substr(inp, start, result-1)
|
|
||||||
|
|
||||||
proc parseHeaders(s: string, L: int): StringTableRef =
|
|
||||||
result = newStringTable()
|
|
||||||
var i = 0
|
|
||||||
while i < L:
|
|
||||||
var key, val: string
|
|
||||||
i = parseWord(s, key, i)+1
|
|
||||||
i = parseWord(s, val, i)+1
|
|
||||||
result[key] = val
|
|
||||||
if s[i] == ',': inc(i)
|
|
||||||
else: raiseScgiError("',' after netstring expected")
|
|
||||||
|
|
||||||
proc recvChar(s: Socket): char =
|
|
||||||
var c: char
|
|
||||||
if recv(s, addr(c), sizeof(c)) == sizeof(c):
|
|
||||||
result = c
|
|
||||||
|
|
||||||
type
|
|
||||||
ScgiState* = object of RootObj ## SCGI state object
|
|
||||||
server: Socket
|
|
||||||
bufLen: int
|
|
||||||
client*: Socket ## the client socket to send data to
|
|
||||||
headers*: StringTableRef ## the parsed headers
|
|
||||||
input*: string ## the input buffer
|
|
||||||
|
|
||||||
|
|
||||||
# Async
|
|
||||||
|
|
||||||
ClientMode = enum
|
|
||||||
ClientReadChar, ClientReadHeaders, ClientReadContent
|
|
||||||
|
|
||||||
AsyncClient = ref object
|
|
||||||
c: AsyncSocket
|
|
||||||
mode: ClientMode
|
|
||||||
dataLen: int
|
|
||||||
headers: StringTableRef ## the parsed headers
|
|
||||||
input: string ## the input buffer
|
|
||||||
|
|
||||||
AsyncScgiStateObj = object
|
|
||||||
handleRequest: proc (client: AsyncSocket,
|
|
||||||
input: string,
|
|
||||||
headers: StringTableRef) {.closure, gcsafe.}
|
|
||||||
asyncServer: AsyncSocket
|
|
||||||
disp: Dispatcher
|
|
||||||
AsyncScgiState* = ref AsyncScgiStateObj
|
|
||||||
|
|
||||||
proc recvBuffer(s: var ScgiState, L: int) =
|
|
||||||
if L > s.bufLen:
|
|
||||||
s.bufLen = L
|
|
||||||
s.input = newString(L)
|
|
||||||
if L > 0 and recv(s.client, cstring(s.input), L) != L:
|
|
||||||
raiseScgiError("could not read all data")
|
|
||||||
setLen(s.input, L)
|
|
||||||
|
|
||||||
proc open*(s: var ScgiState, port = Port(4000), address = "127.0.0.1",
|
|
||||||
reuseAddr = false) =
|
|
||||||
## opens a connection.
|
|
||||||
s.bufLen = 4000
|
|
||||||
s.input = newString(s.bufLen) # will be reused
|
|
||||||
|
|
||||||
s.server = socket()
|
|
||||||
if s.server == invalidSocket: raiseOSError(osLastError())
|
|
||||||
new(s.client) # Initialise s.client for `next`
|
|
||||||
if s.server == invalidSocket: raiseScgiError("could not open socket")
|
|
||||||
#s.server.connect(connectionName, port)
|
|
||||||
if reuseAddr:
|
|
||||||
s.server.setSockOpt(OptReuseAddr, true)
|
|
||||||
bindAddr(s.server, port, address)
|
|
||||||
listen(s.server)
|
|
||||||
|
|
||||||
proc close*(s: var ScgiState) =
|
|
||||||
## closes the connection.
|
|
||||||
s.server.close()
|
|
||||||
|
|
||||||
proc next*(s: var ScgiState, timeout: int = -1): bool =
|
|
||||||
## proceed to the first/next request. Waits ``timeout`` milliseconds for a
|
|
||||||
## request, if ``timeout`` is `-1` then this function will never time out.
|
|
||||||
## Returns `true` if a new request has been processed.
|
|
||||||
var rsocks = @[s.server]
|
|
||||||
if select(rsocks, timeout) == 1 and rsocks.len == 1:
|
|
||||||
new(s.client)
|
|
||||||
accept(s.server, s.client)
|
|
||||||
var L = 0
|
|
||||||
while true:
|
|
||||||
var d = s.client.recvChar()
|
|
||||||
if d == '\0':
|
|
||||||
s.client.close()
|
|
||||||
return false
|
|
||||||
if d notin strutils.Digits:
|
|
||||||
if d != ':': raiseScgiError("':' after length expected")
|
|
||||||
break
|
|
||||||
L = L * 10 + ord(d) - ord('0')
|
|
||||||
recvBuffer(s, L+1)
|
|
||||||
s.headers = parseHeaders(s.input, L)
|
|
||||||
if s.headers.getOrDefault("SCGI") != "1": raiseScgiError("SCGI Version 1 expected")
|
|
||||||
L = parseInt(s.headers.getOrDefault("CONTENT_LENGTH"))
|
|
||||||
recvBuffer(s, L)
|
|
||||||
return true
|
|
||||||
|
|
||||||
proc writeStatusOkTextContent*(c: Socket, contentType = "text/html") =
|
|
||||||
## sends the following string to the socket `c`::
|
|
||||||
##
|
|
||||||
## Status: 200 OK\r\LContent-Type: text/html\r\L\r\L
|
|
||||||
##
|
|
||||||
## You should send this before sending your HTML page, for example.
|
|
||||||
c.send("Status: 200 OK\r\L" &
|
|
||||||
"Content-Type: $1\r\L\r\L" % contentType)
|
|
||||||
|
|
||||||
proc run*(handleRequest: proc (client: Socket, input: string,
|
|
||||||
headers: StringTableRef): bool {.nimcall,gcsafe.},
|
|
||||||
port = Port(4000)) =
|
|
||||||
## encapsulates the SCGI object and main loop.
|
|
||||||
var s: ScgiState
|
|
||||||
s.open(port)
|
|
||||||
var stop = false
|
|
||||||
while not stop:
|
|
||||||
if next(s):
|
|
||||||
stop = handleRequest(s.client, s.input, s.headers)
|
|
||||||
s.client.close()
|
|
||||||
s.close()
|
|
||||||
|
|
||||||
# -- AsyncIO start
|
|
||||||
|
|
||||||
proc recvBufferAsync(client: AsyncClient, L: int): ReadLineResult =
|
|
||||||
result = ReadPartialLine
|
|
||||||
var data = ""
|
|
||||||
if L < 1:
|
|
||||||
raiseScgiError("Cannot read negative or zero length: " & $L)
|
|
||||||
let ret = recvAsync(client.c, data, L)
|
|
||||||
if ret == 0 and data == "":
|
|
||||||
client.c.close()
|
|
||||||
return ReadDisconnected
|
|
||||||
if ret == -1:
|
|
||||||
return ReadNone # No more data available
|
|
||||||
client.input.add(data)
|
|
||||||
if ret == L:
|
|
||||||
return ReadFullLine
|
|
||||||
|
|
||||||
proc checkCloseSocket(client: AsyncClient) =
|
|
||||||
if not client.c.isClosed:
|
|
||||||
if client.c.isSendDataBuffered:
|
|
||||||
client.c.setHandleWrite do (s: AsyncSocket):
|
|
||||||
if not s.isClosed and not s.isSendDataBuffered:
|
|
||||||
s.close()
|
|
||||||
s.delHandleWrite()
|
|
||||||
else: client.c.close()
|
|
||||||
|
|
||||||
proc handleClientRead(client: AsyncClient, s: AsyncScgiState) =
|
|
||||||
case client.mode
|
|
||||||
of ClientReadChar:
|
|
||||||
while true:
|
|
||||||
var d = ""
|
|
||||||
let ret = client.c.recvAsync(d, 1)
|
|
||||||
if d == "" and ret == 0:
|
|
||||||
# Disconnected
|
|
||||||
client.c.close()
|
|
||||||
return
|
|
||||||
if ret == -1:
|
|
||||||
return # No more data available
|
|
||||||
if d[0] notin strutils.Digits:
|
|
||||||
if d[0] != ':': raiseScgiError("':' after length expected")
|
|
||||||
break
|
|
||||||
client.dataLen = client.dataLen * 10 + ord(d[0]) - ord('0')
|
|
||||||
client.mode = ClientReadHeaders
|
|
||||||
handleClientRead(client, s) # Allow progression
|
|
||||||
of ClientReadHeaders:
|
|
||||||
let ret = recvBufferAsync(client, (client.dataLen+1)-client.input.len)
|
|
||||||
case ret
|
|
||||||
of ReadFullLine:
|
|
||||||
client.headers = parseHeaders(client.input, client.input.len-1)
|
|
||||||
if client.headers.getOrDefault("SCGI") != "1": raiseScgiError("SCGI Version 1 expected")
|
|
||||||
client.input = "" # For next part
|
|
||||||
|
|
||||||
let contentLen = parseInt(client.headers.getOrDefault("CONTENT_LENGTH"))
|
|
||||||
if contentLen > 0:
|
|
||||||
client.mode = ClientReadContent
|
|
||||||
else:
|
|
||||||
s.handleRequest(client.c, client.input, client.headers)
|
|
||||||
checkCloseSocket(client)
|
|
||||||
of ReadPartialLine, ReadDisconnected, ReadNone: return
|
|
||||||
of ClientReadContent:
|
|
||||||
let L = parseInt(client.headers.getOrDefault("CONTENT_LENGTH")) -
|
|
||||||
client.input.len
|
|
||||||
if L > 0:
|
|
||||||
let ret = recvBufferAsync(client, L)
|
|
||||||
case ret
|
|
||||||
of ReadFullLine:
|
|
||||||
s.handleRequest(client.c, client.input, client.headers)
|
|
||||||
checkCloseSocket(client)
|
|
||||||
of ReadPartialLine, ReadDisconnected, ReadNone: return
|
|
||||||
else:
|
|
||||||
s.handleRequest(client.c, client.input, client.headers)
|
|
||||||
checkCloseSocket(client)
|
|
||||||
|
|
||||||
proc handleAccept(sock: AsyncSocket, s: AsyncScgiState) =
|
|
||||||
var client: AsyncSocket
|
|
||||||
new(client)
|
|
||||||
accept(s.asyncServer, client)
|
|
||||||
var asyncClient = AsyncClient(c: client, mode: ClientReadChar, dataLen: 0,
|
|
||||||
headers: newStringTable(), input: "")
|
|
||||||
client.handleRead =
|
|
||||||
proc (sock: AsyncSocket) =
|
|
||||||
handleClientRead(asyncClient, s)
|
|
||||||
s.disp.register(client)
|
|
||||||
|
|
||||||
proc open*(handleRequest: proc (client: AsyncSocket,
|
|
||||||
input: string, headers: StringTableRef) {.
|
|
||||||
closure, gcsafe.},
|
|
||||||
port = Port(4000), address = "127.0.0.1",
|
|
||||||
reuseAddr = false): AsyncScgiState =
|
|
||||||
## Creates an ``AsyncScgiState`` object which serves as a SCGI server.
|
|
||||||
##
|
|
||||||
## After the execution of ``handleRequest`` the client socket will be closed
|
|
||||||
## automatically unless it has already been closed.
|
|
||||||
var cres: AsyncScgiState
|
|
||||||
new(cres)
|
|
||||||
cres.asyncServer = asyncSocket()
|
|
||||||
cres.asyncServer.handleAccept = proc (s: AsyncSocket) = handleAccept(s, cres)
|
|
||||||
if reuseAddr:
|
|
||||||
cres.asyncServer.setSockOpt(OptReuseAddr, true)
|
|
||||||
bindAddr(cres.asyncServer, port, address)
|
|
||||||
listen(cres.asyncServer)
|
|
||||||
cres.handleRequest = handleRequest
|
|
||||||
result = cres
|
|
||||||
|
|
||||||
proc register*(d: Dispatcher, s: AsyncScgiState): Delegate {.discardable.} =
|
|
||||||
## Registers ``s`` with dispatcher ``d``.
|
|
||||||
result = d.register(s.asyncServer)
|
|
||||||
s.disp = d
|
|
||||||
|
|
||||||
proc close*(s: AsyncScgiState) =
|
|
||||||
## Closes the ``AsyncScgiState``.
|
|
||||||
s.asyncServer.close()
|
|
||||||
|
|
||||||
when false:
|
|
||||||
var counter = 0
|
|
||||||
proc handleRequest(client: Socket, input: string,
|
|
||||||
headers: StringTableRef): bool {.procvar.} =
|
|
||||||
inc(counter)
|
|
||||||
client.writeStatusOkTextContent()
|
|
||||||
client.send("Hello for the $#th time." % $counter & "\c\L")
|
|
||||||
return false # do not stop processing
|
|
||||||
|
|
||||||
run(handleRequest)
|
|
||||||
|
|
||||||
|
|
@ -1,253 +0,0 @@
|
||||||
#
|
|
||||||
#
|
|
||||||
# Nim's Runtime Library
|
|
||||||
# (c) Copyright 2012 Dominik Picheta
|
|
||||||
#
|
|
||||||
# See the file "copying.txt", included in this
|
|
||||||
# distribution, for details about the copyright.
|
|
||||||
#
|
|
||||||
|
|
||||||
## This module implements the SMTP client protocol as specified by RFC 5321,
|
|
||||||
## this can be used to send mail to any SMTP Server.
|
|
||||||
##
|
|
||||||
## This module also implements the protocol used to format messages,
|
|
||||||
## as specified by RFC 2822.
|
|
||||||
##
|
|
||||||
## Example gmail use:
|
|
||||||
##
|
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
|
||||||
## var msg = createMessage("Hello from Nim's SMTP",
|
|
||||||
## "Hello!.\n Is this awesome or what?",
|
|
||||||
## @["foo@gmail.com"])
|
|
||||||
## let smtpConn = newSmtp(useSsl = true, debug=true)
|
|
||||||
## smtpConn.connect("smtp.gmail.com", Port 465)
|
|
||||||
## smtpConn.auth("username", "password")
|
|
||||||
## smtpConn.sendmail("username@gmail.com", @["foo@gmail.com"], $msg)
|
|
||||||
##
|
|
||||||
##
|
|
||||||
## For SSL support this module relies on OpenSSL. If you want to
|
|
||||||
## enable SSL, compile with ``-d:ssl``.
|
|
||||||
|
|
||||||
import net, strutils, strtabs, base64, os
|
|
||||||
import asyncnet, asyncdispatch
|
|
||||||
|
|
||||||
export Port
|
|
||||||
|
|
||||||
type
|
|
||||||
Message* = object
|
|
||||||
msgTo: seq[string]
|
|
||||||
msgCc: seq[string]
|
|
||||||
msgSubject: string
|
|
||||||
msgOtherHeaders: StringTableRef
|
|
||||||
msgBody: string
|
|
||||||
|
|
||||||
ReplyError* = object of IOError
|
|
||||||
|
|
||||||
SmtpBase[SocketType] = ref object
|
|
||||||
sock: SocketType
|
|
||||||
debug: bool
|
|
||||||
|
|
||||||
Smtp* = SmtpBase[Socket]
|
|
||||||
AsyncSmtp* = SmtpBase[AsyncSocket]
|
|
||||||
|
|
||||||
proc debugSend(smtp: Smtp | AsyncSmtp, cmd: string) {.multisync.} =
|
|
||||||
if smtp.debug:
|
|
||||||
echo("C:" & cmd)
|
|
||||||
await smtp.sock.send(cmd)
|
|
||||||
|
|
||||||
proc debugRecv(smtp: Smtp | AsyncSmtp): Future[TaintedString] {.multisync.} =
|
|
||||||
result = await smtp.sock.recvLine()
|
|
||||||
if smtp.debug:
|
|
||||||
echo("S:" & result.string)
|
|
||||||
|
|
||||||
proc quitExcpt(smtp: Smtp, msg: string) =
|
|
||||||
smtp.debugSend("QUIT")
|
|
||||||
raise newException(ReplyError, msg)
|
|
||||||
|
|
||||||
const compiledWithSsl = defined(ssl)
|
|
||||||
|
|
||||||
when not defined(ssl):
|
|
||||||
type PSSLContext = ref object
|
|
||||||
let defaultSSLContext: PSSLContext = nil
|
|
||||||
else:
|
|
||||||
var defaultSSLContext {.threadvar.}: SSLContext
|
|
||||||
|
|
||||||
proc getSSLContext(): SSLContext =
|
|
||||||
if defaultSSLContext == nil:
|
|
||||||
defaultSSLContext = newContext(verifyMode = CVerifyNone)
|
|
||||||
result = defaultSSLContext
|
|
||||||
|
|
||||||
proc createMessage*(mSubject, mBody: string, mTo, mCc: seq[string],
|
|
||||||
otherHeaders: openarray[tuple[name, value: string]]): Message =
|
|
||||||
## Creates a new MIME compliant message.
|
|
||||||
result.msgTo = mTo
|
|
||||||
result.msgCc = mCc
|
|
||||||
result.msgSubject = mSubject
|
|
||||||
result.msgBody = mBody
|
|
||||||
result.msgOtherHeaders = newStringTable()
|
|
||||||
for n, v in items(otherHeaders):
|
|
||||||
result.msgOtherHeaders[n] = v
|
|
||||||
|
|
||||||
proc createMessage*(mSubject, mBody: string, mTo,
|
|
||||||
mCc: seq[string] = @[]): Message =
|
|
||||||
## Alternate version of the above.
|
|
||||||
result.msgTo = mTo
|
|
||||||
result.msgCc = mCc
|
|
||||||
result.msgSubject = mSubject
|
|
||||||
result.msgBody = mBody
|
|
||||||
result.msgOtherHeaders = newStringTable()
|
|
||||||
|
|
||||||
proc `$`*(msg: Message): string =
|
|
||||||
## stringify for ``Message``.
|
|
||||||
result = ""
|
|
||||||
if msg.msgTo.len() > 0:
|
|
||||||
result = "TO: " & msg.msgTo.join(", ") & "\c\L"
|
|
||||||
if msg.msgCc.len() > 0:
|
|
||||||
result.add("CC: " & msg.msgCc.join(", ") & "\c\L")
|
|
||||||
# TODO: Folding? i.e when a line is too long, shorten it...
|
|
||||||
result.add("Subject: " & msg.msgSubject & "\c\L")
|
|
||||||
for key, value in pairs(msg.msgOtherHeaders):
|
|
||||||
result.add(key & ": " & value & "\c\L")
|
|
||||||
|
|
||||||
result.add("\c\L")
|
|
||||||
result.add(msg.msgBody)
|
|
||||||
|
|
||||||
proc newSmtp*(useSsl = false, debug=false,
|
|
||||||
sslContext: SSLContext = nil): Smtp =
|
|
||||||
## Creates a new ``Smtp`` instance.
|
|
||||||
new result
|
|
||||||
result.debug = debug
|
|
||||||
result.sock = newSocket()
|
|
||||||
if useSsl:
|
|
||||||
when compiledWithSsl:
|
|
||||||
if sslContext == nil:
|
|
||||||
getSSLContext().wrapSocket(result.sock)
|
|
||||||
else:
|
|
||||||
sslContext.wrapSocket(result.sock)
|
|
||||||
else:
|
|
||||||
{.error: "SMTP module compiled without SSL support".}
|
|
||||||
|
|
||||||
proc newAsyncSmtp*(useSsl = false, debug=false,
|
|
||||||
sslContext: SSLContext = nil): AsyncSmtp =
|
|
||||||
## Creates a new ``AsyncSmtp`` instance.
|
|
||||||
new result
|
|
||||||
result.debug = debug
|
|
||||||
|
|
||||||
result.sock = newAsyncSocket()
|
|
||||||
if useSsl:
|
|
||||||
when compiledWithSsl:
|
|
||||||
if sslContext == nil:
|
|
||||||
getSSLContext().wrapSocket(result.sock)
|
|
||||||
else:
|
|
||||||
sslContext.wrapSocket(result.sock)
|
|
||||||
else:
|
|
||||||
{.error: "SMTP module compiled without SSL support".}
|
|
||||||
|
|
||||||
proc quitExcpt(smtp: AsyncSmtp, msg: string): Future[void] =
|
|
||||||
var retFuture = newFuture[void]()
|
|
||||||
var sendFut = smtp.debugSend("QUIT")
|
|
||||||
sendFut.callback =
|
|
||||||
proc () =
|
|
||||||
# TODO: Fix this in async procs.
|
|
||||||
raise newException(ReplyError, msg)
|
|
||||||
return retFuture
|
|
||||||
|
|
||||||
proc checkReply(smtp: Smtp | AsyncSmtp, reply: string) {.multisync.} =
|
|
||||||
var line = await smtp.debugRecv()
|
|
||||||
if not line.startswith(reply):
|
|
||||||
await quitExcpt(smtp, "Expected " & reply & " reply, got: " & line)
|
|
||||||
|
|
||||||
proc connect*(smtp: Smtp | AsyncSmtp,
|
|
||||||
address: string, port: Port) {.multisync.} =
|
|
||||||
## Establishes a connection with a SMTP server.
|
|
||||||
## May fail with ReplyError or with a socket error.
|
|
||||||
await smtp.sock.connect(address, port)
|
|
||||||
|
|
||||||
await smtp.checkReply("220")
|
|
||||||
await smtp.debugSend("HELO " & address & "\c\L")
|
|
||||||
await smtp.checkReply("250")
|
|
||||||
|
|
||||||
proc auth*(smtp: Smtp | AsyncSmtp, username, password: string) {.multisync.} =
|
|
||||||
## Sends an AUTH command to the server to login as the `username`
|
|
||||||
## using `password`.
|
|
||||||
## May fail with ReplyError.
|
|
||||||
|
|
||||||
await smtp.debugSend("AUTH LOGIN\c\L")
|
|
||||||
await smtp.checkReply("334") # TODO: Check whether it's asking for the "Username:"
|
|
||||||
# i.e "334 VXNlcm5hbWU6"
|
|
||||||
await smtp.debugSend(encode(username) & "\c\L")
|
|
||||||
await smtp.checkReply("334") # TODO: Same as above, only "Password:" (I think?)
|
|
||||||
|
|
||||||
await smtp.debugSend(encode(password) & "\c\L")
|
|
||||||
await smtp.checkReply("235") # Check whether the authentification was successful.
|
|
||||||
|
|
||||||
proc sendMail*(smtp: Smtp | AsyncSmtp, fromAddr: string,
|
|
||||||
toAddrs: seq[string], msg: string) {.multisync.} =
|
|
||||||
## Sends ``msg`` from ``fromAddr`` to the addresses specified in ``toAddrs``.
|
|
||||||
## Messages may be formed using ``createMessage`` by converting the
|
|
||||||
## Message into a string.
|
|
||||||
|
|
||||||
await smtp.debugSend("MAIL FROM:<" & fromAddr & ">\c\L")
|
|
||||||
await smtp.checkReply("250")
|
|
||||||
for address in items(toAddrs):
|
|
||||||
await smtp.debugSend("RCPT TO:<" & address & ">\c\L")
|
|
||||||
await smtp.checkReply("250")
|
|
||||||
|
|
||||||
# Send the message
|
|
||||||
await smtp.debugSend("DATA " & "\c\L")
|
|
||||||
await smtp.checkReply("354")
|
|
||||||
await smtp.sock.send(msg & "\c\L")
|
|
||||||
await smtp.debugSend(".\c\L")
|
|
||||||
await smtp.checkReply("250")
|
|
||||||
|
|
||||||
proc close*(smtp: Smtp | AsyncSmtp) {.multisync.} =
|
|
||||||
## Disconnects from the SMTP server and closes the socket.
|
|
||||||
await smtp.debugSend("QUIT\c\L")
|
|
||||||
smtp.sock.close()
|
|
||||||
|
|
||||||
when not defined(testing) and isMainModule:
|
|
||||||
# To test with a real SMTP service, create a smtp.ini file, e.g.:
|
|
||||||
# username = ""
|
|
||||||
# password = ""
|
|
||||||
# smtphost = "smtp.gmail.com"
|
|
||||||
# port = 465
|
|
||||||
# use_tls = true
|
|
||||||
# sender = ""
|
|
||||||
# recipient = ""
|
|
||||||
|
|
||||||
import parsecfg
|
|
||||||
|
|
||||||
proc `[]`(c: Config, key: string): string = c.getSectionValue("", key)
|
|
||||||
|
|
||||||
let
|
|
||||||
conf = loadConfig("smtp.ini")
|
|
||||||
msg = createMessage("Hello from Nim's SMTP!",
|
|
||||||
"Hello!\n Is this awesome or what?", @[conf["recipient"]])
|
|
||||||
|
|
||||||
assert conf["smtphost"] != ""
|
|
||||||
|
|
||||||
proc async_test() {.async.} =
|
|
||||||
let client = newAsyncSmtp(
|
|
||||||
conf["use_tls"].parseBool,
|
|
||||||
debug=true
|
|
||||||
)
|
|
||||||
await client.connect(conf["smtphost"], conf["port"].parseInt.Port)
|
|
||||||
await client.auth(conf["username"], conf["password"])
|
|
||||||
await client.sendMail(conf["sender"], @[conf["recipient"]], $msg)
|
|
||||||
await client.close()
|
|
||||||
echo "async email sent"
|
|
||||||
|
|
||||||
proc sync_test() =
|
|
||||||
var smtpConn = newSmtp(
|
|
||||||
conf["use_tls"].parseBool,
|
|
||||||
debug=true
|
|
||||||
)
|
|
||||||
smtpConn.connect(conf["smtphost"], conf["port"].parseInt.Port)
|
|
||||||
smtpConn.auth(conf["username"], conf["password"])
|
|
||||||
smtpConn.sendMail(conf["sender"], @[conf["recipient"]], $msg)
|
|
||||||
smtpConn.close()
|
|
||||||
echo "sync email sent"
|
|
||||||
|
|
||||||
waitFor async_test()
|
|
||||||
sync_test()
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
-d:ssl
|
|
||||||
|
|
@ -1,406 +0,0 @@
|
||||||
#
|
|
||||||
#
|
|
||||||
# Nim's Runtime Library
|
|
||||||
# (c) Copyright 2012 Andreas Rumpf
|
|
||||||
#
|
|
||||||
# See the file "copying.txt", included in this
|
|
||||||
# distribution, for details about the copyright.
|
|
||||||
#
|
|
||||||
|
|
||||||
## Nim support for `substitution expressions`:idx: (`subex`:idx:).
|
|
||||||
##
|
|
||||||
## .. include:: ../../doc/subexes.txt
|
|
||||||
##
|
|
||||||
|
|
||||||
{.push debugger:off .} # the user does not want to trace a part
|
|
||||||
# of the standard library!
|
|
||||||
|
|
||||||
from strutils import parseInt, cmpIgnoreStyle, Digits
|
|
||||||
include "system/inclrtl"
|
|
||||||
import system/helpers2
|
|
||||||
|
|
||||||
proc findNormalized(x: string, inArray: openarray[string]): int =
|
|
||||||
var i = 0
|
|
||||||
while i < high(inArray):
|
|
||||||
if cmpIgnoreStyle(x, inArray[i]) == 0: return i
|
|
||||||
inc(i, 2) # incrementing by 1 would probably lead to a
|
|
||||||
# security hole...
|
|
||||||
return -1
|
|
||||||
|
|
||||||
type
|
|
||||||
SubexError* = object of ValueError ## exception that is raised for
|
|
||||||
## an invalid subex
|
|
||||||
|
|
||||||
proc raiseInvalidFormat(msg: string) {.noinline.} =
|
|
||||||
raise newException(SubexError, "invalid format string: " & msg)
|
|
||||||
|
|
||||||
type
|
|
||||||
FormatParser = object {.pure, final.}
|
|
||||||
when defined(js):
|
|
||||||
f: string # we rely on the '\0' terminator
|
|
||||||
# which JS's native string doesn't have
|
|
||||||
else:
|
|
||||||
f: cstring
|
|
||||||
num, i, lineLen: int
|
|
||||||
|
|
||||||
template call(x: untyped): untyped =
|
|
||||||
p.i = i
|
|
||||||
x
|
|
||||||
i = p.i
|
|
||||||
|
|
||||||
template callNoLineLenTracking(x: untyped): untyped =
|
|
||||||
let oldLineLen = p.lineLen
|
|
||||||
p.i = i
|
|
||||||
x
|
|
||||||
i = p.i
|
|
||||||
p.lineLen = oldLineLen
|
|
||||||
|
|
||||||
proc getFormatArg(p: var FormatParser, a: openArray[string]): int =
|
|
||||||
const PatternChars = {'a'..'z', 'A'..'Z', '0'..'9', '\128'..'\255', '_'}
|
|
||||||
var i = p.i
|
|
||||||
var f = p.f
|
|
||||||
case f[i]
|
|
||||||
of '#':
|
|
||||||
result = p.num
|
|
||||||
inc i
|
|
||||||
inc p.num
|
|
||||||
of '1'..'9', '-':
|
|
||||||
var j = 0
|
|
||||||
var negative = f[i] == '-'
|
|
||||||
if negative: inc i
|
|
||||||
while f[i] in Digits:
|
|
||||||
j = j * 10 + ord(f[i]) - ord('0')
|
|
||||||
inc i
|
|
||||||
result = if not negative: j-1 else: a.len-j
|
|
||||||
of 'a'..'z', 'A'..'Z', '\128'..'\255', '_':
|
|
||||||
var name = ""
|
|
||||||
while f[i] in PatternChars:
|
|
||||||
name.add(f[i])
|
|
||||||
inc(i)
|
|
||||||
result = findNormalized(name, a)+1
|
|
||||||
of '$':
|
|
||||||
inc(i)
|
|
||||||
call:
|
|
||||||
result = getFormatArg(p, a)
|
|
||||||
result = parseInt(a[result])-1
|
|
||||||
else:
|
|
||||||
raiseInvalidFormat("'#', '$', number or identifier expected")
|
|
||||||
if result >=% a.len: raiseInvalidFormat(formatErrorIndexBound(result, a.len))
|
|
||||||
p.i = i
|
|
||||||
|
|
||||||
proc scanDollar(p: var FormatParser, a: openarray[string], s: var string) {.
|
|
||||||
noSideEffect.}
|
|
||||||
|
|
||||||
proc emitChar(p: var FormatParser, x: var string, ch: char) {.inline.} =
|
|
||||||
x.add(ch)
|
|
||||||
if ch == '\L': p.lineLen = 0
|
|
||||||
else: inc p.lineLen
|
|
||||||
|
|
||||||
proc emitStrLinear(p: var FormatParser, x: var string, y: string) {.inline.} =
|
|
||||||
for ch in items(y): emitChar(p, x, ch)
|
|
||||||
|
|
||||||
proc emitStr(p: var FormatParser, x: var string, y: string) {.inline.} =
|
|
||||||
x.add(y)
|
|
||||||
inc p.lineLen, y.len
|
|
||||||
|
|
||||||
proc scanQuote(p: var FormatParser, x: var string, toAdd: bool) =
|
|
||||||
var i = p.i+1
|
|
||||||
var f = p.f
|
|
||||||
while true:
|
|
||||||
if f[i] == '\'':
|
|
||||||
inc i
|
|
||||||
if f[i] != '\'': break
|
|
||||||
inc i
|
|
||||||
if toAdd: emitChar(p, x, '\'')
|
|
||||||
elif f[i] == '\0': raiseInvalidFormat("closing \"'\" expected")
|
|
||||||
else:
|
|
||||||
if toAdd: emitChar(p, x, f[i])
|
|
||||||
inc i
|
|
||||||
p.i = i
|
|
||||||
|
|
||||||
proc scanBranch(p: var FormatParser, a: openArray[string],
|
|
||||||
x: var string, choice: int) =
|
|
||||||
var i = p.i
|
|
||||||
var f = p.f
|
|
||||||
var c = 0
|
|
||||||
var elsePart = i
|
|
||||||
var toAdd = choice == 0
|
|
||||||
while true:
|
|
||||||
case f[i]
|
|
||||||
of ']': break
|
|
||||||
of '|':
|
|
||||||
inc i
|
|
||||||
elsePart = i
|
|
||||||
inc c
|
|
||||||
if toAdd: break
|
|
||||||
toAdd = choice == c
|
|
||||||
of '\'':
|
|
||||||
call: scanQuote(p, x, toAdd)
|
|
||||||
of '\0': raiseInvalidFormat("closing ']' expected")
|
|
||||||
else:
|
|
||||||
if toAdd:
|
|
||||||
if f[i] == '$':
|
|
||||||
inc i
|
|
||||||
call: scanDollar(p, a, x)
|
|
||||||
else:
|
|
||||||
emitChar(p, x, f[i])
|
|
||||||
inc i
|
|
||||||
else:
|
|
||||||
inc i
|
|
||||||
if not toAdd and choice >= 0:
|
|
||||||
# evaluate 'else' part:
|
|
||||||
var last = i
|
|
||||||
i = elsePart
|
|
||||||
while true:
|
|
||||||
case f[i]
|
|
||||||
of '|', ']': break
|
|
||||||
of '\'':
|
|
||||||
call: scanQuote(p, x, true)
|
|
||||||
of '$':
|
|
||||||
inc i
|
|
||||||
call: scanDollar(p, a, x)
|
|
||||||
else:
|
|
||||||
emitChar(p, x, f[i])
|
|
||||||
inc i
|
|
||||||
i = last
|
|
||||||
p.i = i+1
|
|
||||||
|
|
||||||
proc scanSlice(p: var FormatParser, a: openarray[string]): tuple[x, y: int] =
|
|
||||||
var slice = false
|
|
||||||
var i = p.i
|
|
||||||
var f = p.f
|
|
||||||
|
|
||||||
if f[i] == '{': inc i
|
|
||||||
else: raiseInvalidFormat("'{' expected")
|
|
||||||
if f[i] == '.' and f[i+1] == '.':
|
|
||||||
inc i, 2
|
|
||||||
slice = true
|
|
||||||
else:
|
|
||||||
call: result.x = getFormatArg(p, a)
|
|
||||||
if f[i] == '.' and f[i+1] == '.':
|
|
||||||
inc i, 2
|
|
||||||
slice = true
|
|
||||||
if slice:
|
|
||||||
if f[i] != '}':
|
|
||||||
call: result.y = getFormatArg(p, a)
|
|
||||||
else:
|
|
||||||
result.y = high(a)
|
|
||||||
else:
|
|
||||||
result.y = result.x
|
|
||||||
if f[i] != '}': raiseInvalidFormat("'}' expected")
|
|
||||||
inc i
|
|
||||||
p.i = i
|
|
||||||
|
|
||||||
proc scanDollar(p: var FormatParser, a: openarray[string], s: var string) =
|
|
||||||
var i = p.i
|
|
||||||
var f = p.f
|
|
||||||
case f[i]
|
|
||||||
of '$':
|
|
||||||
emitChar p, s, '$'
|
|
||||||
inc i
|
|
||||||
of '*':
|
|
||||||
for j in 0..a.high: emitStr p, s, a[j]
|
|
||||||
inc i
|
|
||||||
of '{':
|
|
||||||
call:
|
|
||||||
let (x, y) = scanSlice(p, a)
|
|
||||||
for j in x..y: emitStr p, s, a[j]
|
|
||||||
of '[':
|
|
||||||
inc i
|
|
||||||
var start = i
|
|
||||||
call: scanBranch(p, a, s, -1)
|
|
||||||
var x: int
|
|
||||||
if f[i] == '{':
|
|
||||||
inc i
|
|
||||||
call: x = getFormatArg(p, a)
|
|
||||||
if f[i] != '}': raiseInvalidFormat("'}' expected")
|
|
||||||
inc i
|
|
||||||
else:
|
|
||||||
call: x = getFormatArg(p, a)
|
|
||||||
var last = i
|
|
||||||
let choice = parseInt(a[x])
|
|
||||||
i = start
|
|
||||||
call: scanBranch(p, a, s, choice)
|
|
||||||
i = last
|
|
||||||
of '\'':
|
|
||||||
var sep = ""
|
|
||||||
callNoLineLenTracking: scanQuote(p, sep, true)
|
|
||||||
if f[i] == '~':
|
|
||||||
# $' '~{1..3}
|
|
||||||
# insert space followed by 1..3 if not empty
|
|
||||||
inc i
|
|
||||||
call:
|
|
||||||
let (x, y) = scanSlice(p, a)
|
|
||||||
var L = 0
|
|
||||||
for j in x..y: inc L, a[j].len
|
|
||||||
if L > 0:
|
|
||||||
emitStrLinear p, s, sep
|
|
||||||
for j in x..y: emitStr p, s, a[j]
|
|
||||||
else:
|
|
||||||
block StringJoin:
|
|
||||||
block OptionalLineLengthSpecifier:
|
|
||||||
var maxLen = 0
|
|
||||||
case f[i]
|
|
||||||
of '0'..'9':
|
|
||||||
while f[i] in Digits:
|
|
||||||
maxLen = maxLen * 10 + ord(f[i]) - ord('0')
|
|
||||||
inc i
|
|
||||||
of '$':
|
|
||||||
# do not skip the '$' here for `getFormatArg`!
|
|
||||||
call:
|
|
||||||
maxLen = getFormatArg(p, a)
|
|
||||||
else: break OptionalLineLengthSpecifier
|
|
||||||
var indent = ""
|
|
||||||
case f[i]
|
|
||||||
of 'i':
|
|
||||||
inc i
|
|
||||||
callNoLineLenTracking: scanQuote(p, indent, true)
|
|
||||||
|
|
||||||
call:
|
|
||||||
let (x, y) = scanSlice(p, a)
|
|
||||||
if maxLen < 1: emitStrLinear(p, s, indent)
|
|
||||||
var items = 1
|
|
||||||
emitStr p, s, a[x]
|
|
||||||
for j in x+1..y:
|
|
||||||
emitStr p, s, sep
|
|
||||||
if items >= maxLen:
|
|
||||||
emitStrLinear p, s, indent
|
|
||||||
items = 0
|
|
||||||
emitStr p, s, a[j]
|
|
||||||
inc items
|
|
||||||
of 'c':
|
|
||||||
inc i
|
|
||||||
callNoLineLenTracking: scanQuote(p, indent, true)
|
|
||||||
|
|
||||||
call:
|
|
||||||
let (x, y) = scanSlice(p, a)
|
|
||||||
if p.lineLen + a[x].len > maxLen: emitStrLinear(p, s, indent)
|
|
||||||
emitStr p, s, a[x]
|
|
||||||
for j in x+1..y:
|
|
||||||
emitStr p, s, sep
|
|
||||||
if p.lineLen + a[j].len > maxLen: emitStrLinear(p, s, indent)
|
|
||||||
emitStr p, s, a[j]
|
|
||||||
|
|
||||||
else: raiseInvalidFormat("unit 'c' (chars) or 'i' (items) expected")
|
|
||||||
break StringJoin
|
|
||||||
|
|
||||||
call:
|
|
||||||
let (x, y) = scanSlice(p, a)
|
|
||||||
emitStr p, s, a[x]
|
|
||||||
for j in x+1..y:
|
|
||||||
emitStr p, s, sep
|
|
||||||
emitStr p, s, a[j]
|
|
||||||
else:
|
|
||||||
call:
|
|
||||||
var x = getFormatArg(p, a)
|
|
||||||
emitStr p, s, a[x]
|
|
||||||
p.i = i
|
|
||||||
|
|
||||||
|
|
||||||
type
|
|
||||||
Subex* = distinct string ## string that contains a substitution expression
|
|
||||||
|
|
||||||
proc subex*(s: string): Subex =
|
|
||||||
## constructs a *substitution expression* from `s`. Currently this performs
|
|
||||||
## no syntax checking but this may change in later versions.
|
|
||||||
result = Subex(s)
|
|
||||||
|
|
||||||
proc addf*(s: var string, formatstr: Subex, a: varargs[string, `$`]) {.
|
|
||||||
noSideEffect, rtl, extern: "nfrmtAddf".} =
|
|
||||||
## The same as ``add(s, formatstr % a)``, but more efficient.
|
|
||||||
var p: FormatParser
|
|
||||||
p.f = formatstr.string
|
|
||||||
var i = 0
|
|
||||||
while i < len(formatstr.string):
|
|
||||||
if p.f[i] == '$':
|
|
||||||
inc i
|
|
||||||
call: scanDollar(p, a, s)
|
|
||||||
else:
|
|
||||||
emitChar(p, s, p.f[i])
|
|
||||||
inc(i)
|
|
||||||
|
|
||||||
proc `%` *(formatstr: Subex, a: openarray[string]): string {.noSideEffect,
|
|
||||||
rtl, extern: "nfrmtFormatOpenArray".} =
|
|
||||||
## The `substitution`:idx: operator performs string substitutions in
|
|
||||||
## `formatstr` and returns a modified `formatstr`. This is often called
|
|
||||||
## `string interpolation`:idx:.
|
|
||||||
##
|
|
||||||
result = newStringOfCap(formatstr.string.len + a.len shl 4)
|
|
||||||
addf(result, formatstr, a)
|
|
||||||
|
|
||||||
proc `%` *(formatstr: Subex, a: string): string {.noSideEffect,
|
|
||||||
rtl, extern: "nfrmtFormatSingleElem".} =
|
|
||||||
## This is the same as ``formatstr % [a]``.
|
|
||||||
result = newStringOfCap(formatstr.string.len + a.len)
|
|
||||||
addf(result, formatstr, [a])
|
|
||||||
|
|
||||||
proc format*(formatstr: Subex, a: varargs[string, `$`]): string {.noSideEffect,
|
|
||||||
rtl, extern: "nfrmtFormatVarargs".} =
|
|
||||||
## The `substitution`:idx: operator performs string substitutions in
|
|
||||||
## `formatstr` and returns a modified `formatstr`. This is often called
|
|
||||||
## `string interpolation`:idx:.
|
|
||||||
##
|
|
||||||
result = newStringOfCap(formatstr.string.len + a.len shl 4)
|
|
||||||
addf(result, formatstr, a)
|
|
||||||
|
|
||||||
{.pop.}
|
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
from strutils import replace
|
|
||||||
|
|
||||||
proc `%`(formatstr: string, a: openarray[string]): string =
|
|
||||||
result = newStringOfCap(formatstr.len + a.len shl 4)
|
|
||||||
addf(result, formatstr.Subex, a)
|
|
||||||
|
|
||||||
proc `%`(formatstr: string, a: string): string =
|
|
||||||
result = newStringOfCap(formatstr.len + a.len)
|
|
||||||
addf(result, formatstr.Subex, [a])
|
|
||||||
|
|
||||||
|
|
||||||
doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
|
|
||||||
doAssert "$animal eats $food." % ["animal", "The cat", "food", "fish"] ==
|
|
||||||
"The cat eats fish."
|
|
||||||
|
|
||||||
|
|
||||||
doAssert "$[abc|def]# $3 $# $#" % ["17", "b", "c"] == "def c b c"
|
|
||||||
doAssert "$[abc|def]# $3 $# $#" % ["1", "b", "c"] == "def c b c"
|
|
||||||
doAssert "$[abc|def]# $3 $# $#" % ["0", "b", "c"] == "abc c b c"
|
|
||||||
doAssert "$[abc|def|]# $3 $# $#" % ["17", "b", "c"] == " c b c"
|
|
||||||
|
|
||||||
doAssert "$[abc|def|]# $3 $# $#" % ["-9", "b", "c"] == " c b c"
|
|
||||||
doAssert "$1($', '{2..})" % ["f", "a", "b"] == "f(a, b)"
|
|
||||||
|
|
||||||
doAssert "$[$1($', '{2..})|''''|fg'$3']1" % ["7", "a", "b"] == "fg$3"
|
|
||||||
|
|
||||||
doAssert "$[$#($', '{#..})|''''|$3]1" % ["0", "a", "b"] == "0(a, b)"
|
|
||||||
doAssert "$' '~{..}" % "" == ""
|
|
||||||
doAssert "$' '~{..}" % "P0" == " P0"
|
|
||||||
doAssert "${$1}" % "1" == "1"
|
|
||||||
doAssert "${$$-1} $$1" % "1" == "1 $1"
|
|
||||||
|
|
||||||
doAssert(("$#($', '10c'\n '{#..})" % ["doAssert", "longishA", "longish"]).replace(" \n", "\n") ==
|
|
||||||
"""doAssert(
|
|
||||||
longishA,
|
|
||||||
longish)""")
|
|
||||||
|
|
||||||
doAssert(("type MyEnum* = enum\n $', '2i'\n '{..}" % ["fieldA",
|
|
||||||
"fieldB", "FiledClkad", "fieldD", "fieldE", "longishFieldName"]).replace(" \n", "\n") ==
|
|
||||||
strutils.unindent("""
|
|
||||||
type MyEnum* = enum
|
|
||||||
fieldA, fieldB,
|
|
||||||
FiledClkad, fieldD,
|
|
||||||
fieldE, longishFieldName""", 6))
|
|
||||||
|
|
||||||
doAssert subex"$1($', '{2..})" % ["f", "a", "b", "c"] == "f(a, b, c)"
|
|
||||||
|
|
||||||
doAssert subex"$1 $[files|file|files]{1} copied" % ["1"] == "1 file copied"
|
|
||||||
|
|
||||||
doAssert subex"$['''|'|''''|']']#" % "0" == "'|"
|
|
||||||
|
|
||||||
doAssert((subex("type\n Enum = enum\n $', '40c'\n '{..}") % [
|
|
||||||
"fieldNameA", "fieldNameB", "fieldNameC", "fieldNameD"]).replace(" \n", "\n") ==
|
|
||||||
strutils.unindent("""
|
|
||||||
type
|
|
||||||
Enum = enum
|
|
||||||
fieldNameA, fieldNameB, fieldNameC,
|
|
||||||
fieldNameD""", 6))
|
|
||||||
|
|
@ -1,10 +1,15 @@
|
||||||
discard """
|
discard """
|
||||||
errormsg: "type mismatch: got <proc (s: TScgi: ScgiState or AsyncScgiState) | proc (client: AsyncSocket, headers: StringTableRef, input: string){.noSideEffect, gcsafe, locks: 0.}>"
|
errormsg: "type mismatch: got <proc (s: TScgi: ScgiState or AsyncScgiState) | proc (client: AsyncSocket, headers: StringTableRef, input: string){.noSideEffect, gcsafe, locks: 0.}>"
|
||||||
line: 18
|
line: 23
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Fake ScgiState objects, from now-deprecated scgi module
|
||||||
|
type
|
||||||
|
ScgiState* = object of RootObj ## SCGI state object
|
||||||
|
AsyncScgiState* = object of RootObj ## SCGI state object
|
||||||
|
|
||||||
#bug #442
|
#bug #442
|
||||||
import scgi, sockets, asyncio, strtabs
|
import sockets, asyncio, strtabs
|
||||||
proc handleSCGIRequest[TScgi: ScgiState | AsyncScgiState](s: TScgi) =
|
proc handleSCGIRequest[TScgi: ScgiState | AsyncScgiState](s: TScgi) =
|
||||||
discard
|
discard
|
||||||
proc handleSCGIRequest(client: AsyncSocket, headers: StringTableRef,
|
proc handleSCGIRequest(client: AsyncSocket, headers: StringTableRef,
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@ import parseutils
|
||||||
import deques
|
import deques
|
||||||
import sequtils
|
import sequtils
|
||||||
import strutils
|
import strutils
|
||||||
import subexes
|
|
||||||
import tables
|
import tables
|
||||||
import unicode
|
import unicode
|
||||||
import uri
|
import uri
|
||||||
|
|
|
||||||
|
|
@ -161,7 +161,6 @@ lib/impure/db_sqlite.nim
|
||||||
lib/impure/db_odbc.nim
|
lib/impure/db_odbc.nim
|
||||||
lib/pure/db_common.nim
|
lib/pure/db_common.nim
|
||||||
lib/pure/httpclient.nim
|
lib/pure/httpclient.nim
|
||||||
lib/pure/smtp.nim
|
|
||||||
lib/pure/ropes.nim
|
lib/pure/ropes.nim
|
||||||
lib/pure/unidecode/unidecode.nim
|
lib/pure/unidecode/unidecode.nim
|
||||||
lib/pure/xmlparser.nim
|
lib/pure/xmlparser.nim
|
||||||
|
|
@ -171,7 +170,6 @@ lib/pure/colors.nim
|
||||||
lib/pure/mimetypes.nim
|
lib/pure/mimetypes.nim
|
||||||
lib/pure/json.nim
|
lib/pure/json.nim
|
||||||
lib/pure/base64.nim
|
lib/pure/base64.nim
|
||||||
lib/pure/scgi.nim
|
|
||||||
lib/impure/nre.nim
|
lib/impure/nre.nim
|
||||||
lib/impure/nre/private/util.nim
|
lib/impure/nre/private/util.nim
|
||||||
lib/deprecated/pure/sockets.nim
|
lib/deprecated/pure/sockets.nim
|
||||||
|
|
@ -189,11 +187,9 @@ lib/pure/collections/sequtils.nim
|
||||||
lib/pure/collections/rtarrays.nim
|
lib/pure/collections/rtarrays.nim
|
||||||
lib/pure/cookies.nim
|
lib/pure/cookies.nim
|
||||||
lib/pure/memfiles.nim
|
lib/pure/memfiles.nim
|
||||||
lib/pure/subexes.nim
|
|
||||||
lib/pure/collections/critbits.nim
|
lib/pure/collections/critbits.nim
|
||||||
lib/core/locks.nim
|
lib/core/locks.nim
|
||||||
lib/core/rlocks.nim
|
lib/core/rlocks.nim
|
||||||
lib/pure/oids.nim
|
|
||||||
lib/pure/endians.nim
|
lib/pure/endians.nim
|
||||||
lib/pure/uri.nim
|
lib/pure/uri.nim
|
||||||
lib/pure/nimprof.nim
|
lib/pure/nimprof.nim
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue