<commit_before>
import asynchttpserver, asyncdispatch
import httpclient
import strutils
import parsecfg
import options
import os
import nre

const configFilename* = getHomeDir() / ".nimproxy.cfg"
let serverconfig* = loadConfig(configFilename)

proc getServerPort*(): Port =
  let value = serverconfig.getSectionValue("Server", "port")
  if value != "":
    return Port(parseInt(value))
  else:
    return Port(80)

proc findRedirectPathFromConfig*(path: string): Option[string] =
  let value = serverconfig.getSectionValue("Routes", path)
  if value != "":
    return some(value)
  else:
    return none(string)

let rootpathhtmlreg = re"""(src|href)\s*=\s*"\s*(/.*?)""""
let rootpathcssreg = re"""url\(\s*"*(/.*?)"*\)"""
let rootpathjsreg = re""""\s*(/.*?)""""
proc rewriteHTMLRootPath*(src: string, basepath: string): string =
  src.replace(rootpathhtmlreg, "$#=\"/$#$#\"" % ["$1", basepath, "$2"])
proc rewriteCSSRootPath*(src: string, basepath: string): string =
  src.replace(rootpathcssreg, "url(/$#$#)" % [basepath, "$1"])
proc rewriteJSRootPath*(src: string, basepath: string): string =
  src.replace(rootpathjsreg, "\"/$#$#\"" % [basepath, "$1"])
var server = newAsyncHttpServer()
proc handler(req: Request) {.async.} =
  let
    splittedpath = req.url.path.split("/")
    firstpath = splittedpath[1]
    restpath = splittedpath[2..^1]
    lastpath = splittedpath[^1]
    ext = lastpath.splitFile().ext
  let proxypath = findRedirectPathFromConfig(firstpath)
  if proxypath.isSome():
    var client = newHttpClient()
    let path = proxypath.get & "/" & restpath.join("/")
    debugEcho "PROXY TO: ", path

    let resp = client.request(path, req.reqMethod, req.body)
    let respbody = if ext == ".html" or resp.body.find("<html") != -1:
                     resp.body.rewriteHTMLRootPath(firstpath)
                   elif ext == ".css":
                     resp.body.rewriteCSSRootPath(firstpath)
                   elif ext == ".js":
                     resp.body.rewriteJSRootPath(firstpath)
                   else:
                     resp.body
    await req.respond(resp.code, respbody)
  else:
    await req.respond(Http404, "couldn't find path")
waitfor server.serve(getServerPort(), handler)<commit_msg>delete root path rewrite proc of js<commit_after>
import asynchttpserver, asyncdispatch
import httpclient
import strutils
import parsecfg
import options
import os
import nre

const configFilename* = getHomeDir() / ".nimproxy.cfg"
let serverconfig* = loadConfig(configFilename)

proc getServerPort*(): Port =
  let value = serverconfig.getSectionValue("Server", "port")
  if value != "":
    return Port(parseInt(value))
  else:
    return Port(80)

proc findRedirectPathFromConfig*(path: string): Option[string] =
  let value = serverconfig.getSectionValue("Routes", path)
  if value != "":
    return some(value)
  else:
    return none(string)

let rootpathhtmlreg = re"""(src|href)\s*=\s*"\s*(/.*?)""""
let rootpathcssreg = re"""url\(\s*"*(/.*?)"*\)"""
proc rewriteHTMLRootPath*(src: string, basepath: string): string =
  src.replace(rootpathhtmlreg, "$#=\"/$#$#\"" % ["$1", basepath, "$2"])
proc rewriteCSSRootPath*(src: string, basepath: string): string =
  src.replace(rootpathcssreg, "url(/$#$#)" % [basepath, "$1"])
var server = newAsyncHttpServer()
proc handler(req: Request) {.async.} =
  let
    splittedpath = req.url.path.split("/")
    firstpath = splittedpath[1]
    restpath = splittedpath[2..^1]
    lastpath = splittedpath[^1]
    ext = lastpath.splitFile().ext
  let proxypath = findRedirectPathFromConfig(firstpath)
  if proxypath.isSome():
    var client = newHttpClient()
    let path = proxypath.get & "/" & restpath.join("/")
    debugEcho "PROXY TO: ", path

    let resp = client.request(path, req.reqMethod, req.body)
    let respbody = if ext == ".html" or resp.body.find("<html") != -1:
                     resp.body.rewriteHTMLRootPath(firstpath)
                   elif ext == ".css":
                     resp.body.rewriteCSSRootPath(firstpath)
                   else:
                     resp.body
    await req.respond(resp.code, respbody)
  else:
    await req.respond(Http404, "couldn't find path")
waitfor server.serve(getServerPort(), handler)
{'fraction_non_alphanumeric': 0.15890736342042755, 'fraction_numerical': 0.006413301662707839, 'mean_word_length': 3.5085653104925054, 'pattern_counts': {'":': 7, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1913966', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1357, 'n_words': 309}