summary refs log tree commit diff stats
path: root/compiler/reorder.nim
Commit message (Collapse)AuthorAgeFilesLines
* fixes addr/hiddenAddr in strictdefs (#23477)ringabout2024-04-101-1/+1
|
* prepare for the enforcement of `std` prefix (#22873)ringabout2023-10-291-1/+3
| | | follow up https://github.com/nim-lang/Nim/pull/22851
* follow up #22549 (#22551)ringabout2023-08-251-0/+1
|
* modernize compiler/reorder, which exposes yet another strictdefs bug (#22415)ringabout2023-08-081-13/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ```nim {.experimental: "strictdefs".} type NodeKind = enum nkImportStmt nkStmtList nkNone PNode = ref object kind: NodeKind proc hasImportStmt(n: PNode): bool = # Checks if the node is an import statement or # i it contains one case n.kind of nkImportStmt: return true of nkStmtList: if false: return true else: result = false var n = PNode() echo hasImportStmt(n) ``` It compiles without warnings, but shouldn't. As a contrast, ```nim {.experimental: "strictdefs".} type NodeKind = enum nkImportStmt nkStmtList nkNone PNode = ref object kind: NodeKind proc hasImportStmt(n: PNode): bool = # Checks if the node is an import statement or # i it contains one case n.kind of nkImportStmt: result = true of nkStmtList: if false: return true else: result = false var n = PNode() echo hasImportStmt(n) ``` This gives a proper warning.
* use strictdefs for compiler (#22365)ringabout2023-08-061-0/+7
| | | | | | | | | | | | | | | * wip; use strictdefs for compiler * checkpoint * complete the chores * more fixes * first phase cleanup * Update compiler/bitsets.nim * cleanup
* fixes pragmas reorder (#21205)ringabout2023-01-011-0/+1
|
* Fix compiler build with -d:nimDebugReorder (#20888)Kyle Coffey2022-11-221-12/+15
| | | | | | | * add missing import for -d:debugReorder * rename compile-time symbol to match best practices Fix #20887.
* move assertions out of system (#19599)flywind2022-03-231-0/+3
|
* ast: add getPIdent (#17684)Timothee Cour2021-04-091-5/+2
| | | | | * ast: add getPIdent * fixup
* potential fix for semgeneric formal params (#17494)Saem Ghani2021-03-241-0/+1
| | | | marked locations where analysis of return formal param is done prior to args. This might fix some subtle bugs.
* fixes #11225; generic sandwich problems; [backport:1.2] (#17255)Andreas Rumpf2021-03-091-0/+1
| | | | | * fixes #11225; generic sandwich problems; [backport:1.2] * progress * delegating these symbols must be done via 'bind'
* Fix Typos (#17194)Juan Carlos2021-02-271-1/+1
|
* Big compiler Cleanup (#14777)Clyybber2020-08-281-25/+2
|
* reorder.nim: fixed typosAraq2020-06-121-3/+3
|
* reorder.nim: fixes the indentationAraq2020-06-061-41/+40
|
* Cosmetic compiler cleanup (#12718)Clyybber2019-11-281-15/+15
| | | | | | | | | | | | | | | | | | * Cleanup compiler code base * Unify add calls * Unify len invocations * Unify range operators * Fix oversight * Remove {.procvar.} pragma * initCandidate -> newCandidate where reasonable * Unify safeLen calls
* [refactoring] remove unused imports in the compiler and in some stdlib modulesAraq2019-07-181-2/+2
|
* styleCheck: make the compiler and large parts of the stdlib compatible with ↵Araq2019-07-101-1/+1
| | | | --styleCheck:error
* [refactor] compiler/[msgs, reorder, semstmts]: use toMsgFilename where ↵alaviss2019-06-261-1/+1
| | | | | | | | appropriate (#11595) * compiler/msgs: toMsgFilename now operates on FileIndex * compiler/reorder: use toMsgFilename for compiler messages * compiler/semstmts: respect listFullPaths for recursive deps error
* Replace countup(x, y) with x .. yClyybber2019-05-071-1/+1
|
* fix typo in codeReordering error msg (#10667)Timothee Cour2019-02-141-1/+1
| | | | | | * fix typo in codeReordering * reorder=>codeReordering
* deprecated ospaths (#9665)Andreas Rumpf2018-11-091-1/+1
|
* fixes #9281Araq2018-10-111-1/+1
|
* incremental compilation: implemented basic replay logicAndreas Rumpf2018-06-021-10/+8
|
* refactoring: remove idents.legacy global variable and pass the IdentCache ↵Andreas Rumpf2018-05-271-13/+13
| | | | around explicitly
* refactoring: fewer global variablesAndreas Rumpf2018-05-271-12/+12
|
* remove more global variables in the Nim compilerAndreas Rumpf2018-05-271-1/+1
|
* preparations of making compiler/msgs.nim free of global variablesAndreas Rumpf2018-05-171-2/+3
|
* big refactoring: mores stuff compilesAndreas Rumpf2018-05-101-6/+7
|
* compiler refactoring, pass config around explicitlyAndreas Rumpf2018-05-051-4/+4
|
* compiler: remove unnecessary FileIndex type conversionsAndreas Rumpf2018-04-241-1/+1
|
* refactoring: make FileIndex a distinct type; make line information an ↵Andreas Rumpf2018-04-211-10/+10
| | | | uint16; fixes #7654
* deprecated unary '<'Andreas Rumpf2017-10-291-1/+1
|
* Add sections (type, var, let, const, using) support for reorder pragma (#6326)BigEpsilon2017-10-281-43/+385
|
* fixes #6306Andreas Rumpf2017-09-011-1/+1
|
* reorder pass: consider the 'using' statement; disabled debug outputAndreas Rumpf2017-08-111-2/+2
|
* implemented reordering passAndreas Rumpf2017-07-251-0/+102
title='Blame the previous revision' href='/ahoang/Nim/blame/lib/pure/httpcore.nim?h=devel&id=1740619c0cd3f94c2fe16560d44402daca449e53'>^
cfd187d16 ^
3ad368f8c ^
26c7a76c8 ^
159643824 ^
26c7a76c8 ^
cc80eac84 ^







7c1512024 ^





a336bf239 ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328



















                                                               


                                                                              




                     














                                                                              
                                                                         
                                                                 

                                                                     



                                                                              

















































                          









                                                              
                                                     


                                                     


                                         










                                                                           
                                                         




                                                               

                                                                           
                            
                                           



                                                                            
                                            




                                                                             
                                         



                                                                           

                                                
       
                                              
 



                                                      









                                                                               
                                                          

                                                       
                                                 

































                                                                             

                          













                                                              


                                                           
                                   




                                                            
                                                



















































                                               
                                                   
                                           


                                                           
                                 


                                                           
                                 


                                                           
                                 


                                                           
                                 
 
                                           
                                                         
 







                                             





                                                       


                                                     
#
#
#            Nim's Runtime Library
#        (c) Copyright 2016 Dominik Picheta
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Contains functionality shared between the ``httpclient`` and
## ``asynchttpserver`` modules.

import tables, strutils, parseutils

type
  HttpHeaders* = ref object
    table*: TableRef[string, seq[string]]

  HttpHeaderValues* = distinct seq[string]

  # The range starts at '0' so that we don't have to explicitly initialise
  # it. See: http://irclogs.nim-lang.org/19-09-2016.html#19:48:27 for context.
  HttpCode* = distinct range[0 .. 599]

  HttpVersion* = enum
    HttpVer11,
    HttpVer10

  HttpMethod* = enum  ## the requested HttpMethod
    HttpHead,         ## Asks for the response identical to the one that would
                      ## correspond to a GET request, but without the response
                      ## body.
    HttpGet,          ## Retrieves the specified resource.
    HttpPost,         ## Submits data to be processed to the identified
                      ## resource. The data is included in the body of the
                      ## request.
    HttpPut,          ## Uploads a representation of the specified resource.
    HttpDelete,       ## Deletes the specified resource.
    HttpTrace,        ## Echoes back the received request, so that a client
                      ## can see what intermediate servers are adding or
                      ## changing in the request.
    HttpOptions,      ## Returns the HTTP methods that the server supports
                      ## for specified address.
    HttpConnect,      ## Converts the request connection to a transparent
                      ## TCP/IP tunnel, usually used for proxies.
    HttpPatch         ## Applies partial modifications to a resource.

{.deprecated: [httpGet: HttpGet, httpHead: HttpHead, httpPost: HttpPost,
               httpPut: HttpPut, httpDelete: HttpDelete, httpTrace: HttpTrace,
               httpOptions: HttpOptions, httpConnect: HttpConnect].}


const
  Http100* = HttpCode(100)
  Http101* = HttpCode(101)
  Http200* = HttpCode(200)
  Http201* = HttpCode(201)
  Http202* = HttpCode(202)
  Http203* = HttpCode(203)
  Http204* = HttpCode(204)
  Http205* = HttpCode(205)
  Http206* = HttpCode(206)
  Http300* = HttpCode(300)
  Http301* = HttpCode(301)
  Http302* = HttpCode(302)
  Http303* = HttpCode(303)
  Http304* = HttpCode(304)
  Http305* = HttpCode(305)
  Http307* = HttpCode(307)
  Http400* = HttpCode(400)
  Http401* = HttpCode(401)
  Http403* = HttpCode(403)
  Http404* = HttpCode(404)
  Http405* = HttpCode(405)
  Http406* = HttpCode(406)
  Http407* = HttpCode(407)
  Http408* = HttpCode(408)
  Http409* = HttpCode(409)
  Http410* = HttpCode(410)
  Http411* = HttpCode(411)
  Http412* = HttpCode(412)
  Http413* = HttpCode(413)
  Http414* = HttpCode(414)
  Http415* = HttpCode(415)
  Http416* = HttpCode(416)
  Http417* = HttpCode(417)
  Http418* = HttpCode(418)
  Http421* = HttpCode(421)
  Http422* = HttpCode(422)
  Http426* = HttpCode(426)
  Http428* = HttpCode(428)
  Http429* = HttpCode(429)
  Http431* = HttpCode(431)
  Http451* = HttpCode(451)
  Http500* = HttpCode(500)
  Http501* = HttpCode(501)
  Http502* = HttpCode(502)
  Http503* = HttpCode(503)
  Http504* = HttpCode(504)
  Http505* = HttpCode(505)

const headerLimit* = 10_000

proc newHttpHeaders*(): HttpHeaders =
  new result
  result.table = newTable[string, seq[string]]()

proc newHttpHeaders*(keyValuePairs:
    openarray[tuple[key: string, val: string]]): HttpHeaders =
  var pairs: seq[tuple[key: string, val: seq[string]]] = @[]
  for pair in keyValuePairs:
    pairs.add((pair.key.toLowerAscii(), @[pair.val]))
  new result
  result.table = newTable[string, seq[string]](pairs)

proc `$`*(headers: HttpHeaders): string =
  return $headers.table

proc clear*(headers: HttpHeaders) =
  headers.table.clear()

proc `[]`*(headers: HttpHeaders, key: string): HttpHeaderValues =
  ## Returns the values associated with the given ``key``. If the returned
  ## values are passed to a procedure expecting a ``string``, the first
  ## value is automatically picked. If there are
  ## no values associated with the key, an exception is raised.
  ##
  ## To access multiple values of a key, use the overloaded ``[]`` below or
  ## to get all of them access the ``table`` field directly.
  return headers.table[key.toLowerAscii].HttpHeaderValues

converter toString*(values: HttpHeaderValues): string =
  return seq[string](values)[0]

proc `[]`*(headers: HttpHeaders, key: string, i: int): string =
  ## Returns the ``i``'th value associated with the given key. If there are
  ## no values associated with the key or the ``i``'th value doesn't exist,
  ## an exception is raised.
  return headers.table[key.toLowerAscii][i]

proc `[]=`*(headers: HttpHeaders, key, value: string) =
  ## Sets the header entries associated with ``key`` to the specified value.
  ## Replaces any existing values.
  headers.table[key.toLowerAscii] = @[value]

proc `[]=`*(headers: HttpHeaders, key: string, value: seq[string]) =
  ## Sets the header entries associated with ``key`` to the specified list of
  ## values.
  ## Replaces any existing values.
  headers.table[key.toLowerAscii] = value

proc add*(headers: HttpHeaders, key, value: string) =
  ## Adds the specified value to the specified key. Appends to any existing
  ## values associated with the key.
  if not headers.table.hasKey(key.toLowerAscii):
    headers.table[key.toLowerAscii] = @[value]
  else:
    headers.table[key.toLowerAscii].add(value)

proc del*(headers: HttpHeaders, key: string) =
  ## Delete the header entries associated with ``key``
  headers.table.del(key.toLowerAscii)

iterator pairs*(headers: HttpHeaders): tuple[key, value: string] =
  ## Yields each key, value pair.
  for k, v in headers.table:
    for value in v:
      yield (k, value)

proc contains*(values: HttpHeaderValues, value: string): bool =
  ## Determines if ``value`` is one of the values inside ``values``. Comparison
  ## is performed without case sensitivity.
  for val in seq[string](values):
    if val.toLowerAscii == value.toLowerAscii: return true

proc hasKey*(headers: HttpHeaders, key: string): bool =
  return headers.table.hasKey(key.toLowerAscii())

proc getOrDefault*(headers: HttpHeaders, key: string,
    default = @[""].HttpHeaderValues): HttpHeaderValues =
  ## Returns the values associated with the given ``key``. If there are no
  ## values associated with the key, then ``default`` is returned.
  if headers.hasKey(key):
    return headers[key]
  else:
    return default

proc len*(headers: HttpHeaders): int = return headers.table.len

proc parseList(line: string, list: var seq[string], start: int): int =
  var i = 0
  var current = ""
  while line[start + i] notin {'\c', '\l', '\0'}:
    i += line.skipWhitespace(start + i)
    i += line.parseUntil(current, {'\c', '\l', ','}, start + i)
    list.add(current)
    if line[start + i] == ',':
      i.inc # Skip ,
    current.setLen(0)

proc parseHeader*(line: string): tuple[key: string, value: seq[string]] =
  ## Parses a single raw header HTTP line into key value pairs.
  ##
  ## Used by ``asynchttpserver`` and ``httpclient`` internally and should not
  ## be used by you.
  result.value = @[]
  var i = 0
  i = line.parseUntil(result.key, ':')
  inc(i) # skip :
  if i < len(line):
    i += parseList(line, result.value, i)
  elif result.key.len > 0:
    result.value = @[""]
  else:
    result.value = @[]

proc `==`*(protocol: tuple[orig: string, major, minor: int],
           ver: HttpVersion): bool =
  let major =
    case ver
    of HttpVer11, HttpVer10: 1
  let minor =
    case ver
    of HttpVer11: 1
    of HttpVer10: 0
  result = protocol.major == major and protocol.minor == minor

proc contains*(methods: set[HttpMethod], x: string): bool =
  return parseEnum[HttpMethod](x) in methods

proc `$`*(code: HttpCode): string =
  ## Converts the specified ``HttpCode`` into a HTTP status.
  ##
  ## For example:
  ##
  ##   .. code-block:: nim
  ##       doAssert($Http404 == "404 Not Found")
  case code.int
  of 100: "100 Continue"
  of 101: "101 Switching Protocols"
  of 200: "200 OK"
  of 201: "201 Created"
  of 202: "202 Accepted"
  of 203: "203 Non-Authoritative Information"
  of 204: "204 No Content"
  of 205: "205 Reset Content"
  of 206: "206 Partial Content"
  of 300: "300 Multiple Choices"
  of 301: "301 Moved Permanently"
  of 302: "302 Found"
  of 303: "303 See Other"
  of 304: "304 Not Modified"
  of 305: "305 Use Proxy"
  of 307: "307 Temporary Redirect"
  of 400: "400 Bad Request"
  of 401: "401 Unauthorized"
  of 403: "403 Forbidden"
  of 404: "404 Not Found"
  of 405: "405 Method Not Allowed"
  of 406: "406 Not Acceptable"
  of 407: "407 Proxy Authentication Required"
  of 408: "408 Request Timeout"
  of 409: "409 Conflict"
  of 410: "410 Gone"
  of 411: "411 Length Required"
  of 412: "412 Precondition Failed"
  of 413: "413 Request Entity Too Large"
  of 414: "414 Request-URI Too Long"
  of 415: "415 Unsupported Media Type"
  of 416: "416 Requested Range Not Satisfiable"
  of 417: "417 Expectation Failed"
  of 418: "418 I'm a teapot"
  of 421: "421 Misdirected Request"
  of 422: "422 Unprocessable Entity"
  of 426: "426 Upgrade Required"
  of 428: "428 Precondition Required"
  of 429: "429 Too Many Requests"
  of 431: "431 Request Header Fields Too Large"
  of 451: "451 Unavailable For Legal Reasons"
  of 500: "500 Internal Server Error"
  of 501: "501 Not Implemented"
  of 502: "502 Bad Gateway"
  of 503: "503 Service Unavailable"
  of 504: "504 Gateway Timeout"
  of 505: "505 HTTP Version Not Supported"
  else: $(int(code))

proc `==`*(a, b: HttpCode): bool {.borrow.}

proc `==`*(rawCode: string, code: HttpCode): bool =
  return cmpIgnoreCase(rawCode, $code) == 0

proc is2xx*(code: HttpCode): bool =
  ## Determines whether ``code`` is a 2xx HTTP status code.
  return code.int in {200 .. 299}

proc is3xx*(code: HttpCode): bool =
  ## Determines whether ``code`` is a 3xx HTTP status code.
  return code.int in {300 .. 399}

proc is4xx*(code: HttpCode): bool =
  ## Determines whether ``code`` is a 4xx HTTP status code.
  return code.int in {400 .. 499}

proc is5xx*(code: HttpCode): bool =
  ## Determines whether ``code`` is a 5xx HTTP status code.
  return code.int in {500 .. 599}

proc `$`*(httpMethod: HttpMethod): string =
  return (system.`$`(httpMethod))[4 .. ^1].toUpperAscii()

when isMainModule:
  var test = newHttpHeaders()
  test["Connection"] = @["Upgrade", "Close"]
  doAssert test["Connection", 0] == "Upgrade"
  doAssert test["Connection", 1] == "Close"
  test.add("Connection", "Test")
  doAssert test["Connection", 2] == "Test"
  doAssert "upgrade" in test["Connection"]

  # Bug #5344.
  doAssert parseHeader("foobar: ") == ("foobar", @[""])
  let (key, value) = parseHeader("foobar: ")
  test = newHttpHeaders()
  test[key] = value
  doAssert test["foobar"] == ""

  doAssert parseHeader("foobar:") == ("foobar", @[""])