<commit_before>#lang scheme/base
(require mzlib/contract
         mzlib/plt-match
         mzlib/string)
(require "util.ss"
         web-server/http)
(provide/contract
 [read-mime-types (path-string? . -> . hash?)]
 [make-path->mime-type (path-string? . -> . (path? . -> . bytes?))])

; read-mime-types : path? -> hash-table?
(define (read-mime-types a-path)
  (define MIME-TYPE-TABLE (make-hasheq))
  (with-input-from-file a-path
    (lambda ()
      (let loop ()
        (match (read-line (current-input-port) 'any)
          [(? eof-object?)
           (void)]
          [(regexp #"^([^\t ]+)[\t ]+(.+)$"
                   (list s type exts))
           (for-each (lambda (ext)
                       (hash-set! MIME-TYPE-TABLE
                                  (lowercase-symbol! ext)
                                  type))
                     (regexp-split #" " exts))
           (loop)]
          [_
           (loop)]))))
  MIME-TYPE-TABLE)

;; make-get-mime-type : path? -> path? -> bytes?
;; determine the mime type based on the filename's suffix
;;
;; Notes (GregP):
;; 1. Can we determine the mime type based on file contents?
;; 2. Assuming that 7-bit ASCII is correct for mime-type
(define (make-path->mime-type a-path)
  (define MIME-TYPE-TABLE (read-mime-types a-path))
  (define file-suffix-regexp (byte-regexp #".*\\.([^\\.]*$)"))
  (lambda (path)
    (match (regexp-match file-suffix-regexp (path->bytes path))
      [(list path-bytes sffx)
       (hash-ref MIME-TYPE-TABLE
                 (lowercase-symbol! sffx)
                 (lambda () TEXT/HTML-MIME-TYPE))]
      [_ TEXT/HTML-MIME-TYPE])))
<commit_msg>* revise requires to scheme/* * use `for' and better use of regexp patterns in `match' * use a literal byte-regexp in `make-path->mime-type' and use it   throught `match' * delay reading of the mime types table (since there are uses like   serve/servlet that will never use it anyway)<commit_after>#lang scheme/base
(require scheme/contract
         scheme/match
         scheme/promise)
(require "util.ss"
         web-server/http)
(provide/contract
 [read-mime-types (path-string? . -> . hash?)]
 [make-path->mime-type (path-string? . -> . (path? . -> . bytes?))])

; read-mime-types : path? -> hash-table?
(define (read-mime-types a-path)
  (define MIME-TYPE-TABLE (make-hasheq))
  (with-input-from-file a-path
    (lambda ()
      (let loop ()
        (match (read-line (current-input-port) 'any)
          [(? eof-object?)
           (void)]
          [(regexp #rx#"^([^\t ]+)[\t ]+(.+)$" (list _ type exts))
           (for ([ext (in-list (regexp-split #" " exts))])
             (hash-set! MIME-TYPE-TABLE (lowercase-symbol! ext) type))
           (loop)]
          [_
           (loop)]))))
  MIME-TYPE-TABLE)

;; make-get-mime-type : path? -> path? -> bytes?
;; determine the mime type based on the filename's suffix
;;
;; Notes (GregP):
;; 1. Can we determine the mime type based on file contents?
;; 2. Assuming that 7-bit ASCII is correct for mime-type
(define (make-path->mime-type a-path)
  (define MIME-TYPE-TABLE (delay (read-mime-types a-path)))
  (lambda (path)
    (match (path->bytes path)
      [(regexp #rx#".*\\.([^\\.]*$)" (list _ sffx))
       (hash-ref (force MIME-TYPE-TABLE)
                 (lowercase-symbol! sffx)
                 TEXT/HTML-MIME-TYPE)]
      [_ TEXT/HTML-MIME-TYPE])))

{'fraction_non_alphanumeric': 0.174276335422262, 'fraction_numerical': 0.0017905102954341987, 'mean_word_length': 2.144465290806754, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2353997', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1113, 'n_words': 278}