about summary refs log blame commit diff stats
path: root/src/types/mime.nim
blob: cd4c2d51c2b620b34db236ee4a1b5ea3b5f12b3b (plain) (tree)
1
2
3
4
5
6
7
8
9
                







                                     
                        
                      
                    

           
                                                                      



                      
                







                                
            





















                                               
import algorithm
import tables

const DefaultGuess = [
  ("html", "text/html"),
  ("htm", "text/html"),
  ("xhtml", "application/xhtml+xml"),
  ("xhtm", "application/xhtml+xml"),
  ("xht", "application/xhtml+xml"),
  ("txt", "text/plain"),
  ("css", "text/css"),
  ("", "text/plain")
].toTable()

proc guessContentType*(path: string, def = DefaultGuess[""]): string =
  var i = path.len - 1
  var n = 0
  while i > 0:
    if path[i] == '/':
      return def
    if path[i] == '.':
      n = i
      break
    dec i
  if n > 0:
    let ext = path.substr(n + 1)
    if ext in DefaultGuess:
      return DefaultGuess[ext]
  return def

const JavaScriptTypes = [
  "application/ecmascript",
  "application/javascript",
  "application/x-ecmascript",
  "application/x-javascript",
  "text/ecmascript",
  "text/javascript",
  "text/javascript1.0",
  "text/javascript1.1",
  "text/javascript1.2",
  "text/javascript1.3",
  "text/javascript1.4",
  "text/javascript1.5",
  "text/jscript",
  "text/livescript",
  "text/x-ecmascript",
  "text/x-javascript"
]

proc isJavaScriptType*(s: string): bool =
  return binarySearch(JavaScriptTypes, s) != -1