<commit_before>/*
 * Javalin - https://javalin.io
 * Copyright 2017 <NAME>
 * Licensed under Apache 2.0: https://github.com/tipsy/javalin/blob/master/LICENSE
 */

package io.javalin.core.util

import io.javalin.Request
import io.javalin.core.HandlerEntry
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.UnsupportedEncodingException
import java.net.URLDecoder
import java.nio.charset.Charset
import java.util.*
import javax.servlet.http.HttpServletRequest

object RequestUtil {

    fun create(httpRequest: HttpServletRequest): Request {
        return Request(httpRequest, HashMap<String, String>(), ArrayList<String>())
    }

    fun create(httpRequest: HttpServletRequest, handlerEntry: HandlerEntry, requestUri: String): Request {
        val requestList = Util.pathToList(requestUri)
        val matchedList = Util.pathToList(handlerEntry.path)
        return Request(
                httpRequest,
                getParams(requestList, matchedList),
                getSplat(requestList, matchedList)
        )
    }

    fun getSplat(request: List<String>, matched: List<String>): List<String> {
        val numRequestParts = request.size
        val numMatchedParts = matched.size
        val sameLength = numRequestParts == numMatchedParts
        val splat = ArrayList<String>()
        var i = 0
        while (i < numRequestParts && i < numMatchedParts) {
            val matchedPart = matched[i]
            if (isSplat(matchedPart)) {
                val splatParam = StringBuilder(request[i])
                if (!sameLength && i == numMatchedParts - 1) {
                    for (j in i + 1..numRequestParts - 1) {
                        splatParam.append("/")
                        splatParam.append(request[j])
                    }
                }
                splat.add(urlDecode(splatParam.toString()))
            }
            i++
        }
        return splat
    }

    fun getParams(requestPaths: List<String>, handlerPaths: List<String>): Map<String, String> {
        val params = HashMap<String, String>()
        var i = 0
        while (i < requestPaths.size && i < handlerPaths.size) {
            val matchedPart = handlerPaths[i]
            if (isParam(matchedPart)) {
                params[matchedPart.toLowerCase()] = urlDecode(requestPaths[i])
            }
            i++
        }
        return params
    }

    fun urlDecode(s: String): String {
        try {
            return URLDecoder.decode(s.replace("+", "%2B"), "UTF-8").replace("%2B", "+")
        } catch (ignored: UnsupportedEncodingException) {
            return ""
        }
    }

    fun isParam(pathPart: String): Boolean {
        return pathPart.startsWith(":")
    }

    fun isSplat(pathPart: String): Boolean {
        return pathPart == "*"
    }

    fun byteArrayToString(bytes: ByteArray, encoding: String?): String {
        var string: String
        if (encoding != null && Charset.isSupported(encoding)) {
            try {
                string = String(bytes, Charset.forName(encoding))
            } catch (e: UnsupportedEncodingException) {
                string = String(bytes)
            }

        } else {
            string = String(bytes)
        }
        return string
    }

    @Throws(IOException::class)
    fun toByteArray(input: InputStream): ByteArray {
        val baos = ByteArrayOutputStream()
        val byteBuffer = ByteArray(1024)
        var b = input.read(byteBuffer)
        while (b != -1) {
            baos.write(byteBuffer, 0, b)
            b = input.read(byteBuffer)
        }
        return baos.toByteArray()
    }
}
<commit_msg>[core] Minor request-util refactors<commit_after>/*
 * Javalin - https://javalin.io
 * Copyright 2017 <NAME>
 * Licensed under Apache 2.0: https://github.com/tipsy/javalin/blob/master/LICENSE
 */

package io.javalin.core.util

import io.javalin.Request
import io.javalin.core.HandlerEntry
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.UnsupportedEncodingException
import java.net.URLDecoder
import java.nio.charset.Charset
import java.util.*
import javax.servlet.http.HttpServletRequest

object RequestUtil {

    fun create(httpRequest: HttpServletRequest): Request {
        return Request(httpRequest, HashMap<String, String>(), ArrayList<String>())
    }

    fun create(httpRequest: HttpServletRequest, handlerEntry: HandlerEntry, requestUri: String): Request {
        val requestList = Util.pathToList(requestUri)
        val matchedList = Util.pathToList(handlerEntry.path)
        return Request(
                httpRequest,
                getParams(requestList, matchedList),
                getSplat(requestList, matchedList)
        )
    }

    fun getSplat(request: List<String>, matched: List<String>): List<String> {
        val numRequestParts = request.size
        val numHandlerParts = matched.size
        val splat = ArrayList<String>()
        var i = 0
        while (i < numRequestParts && i < numHandlerParts) {
            val matchedPart = matched[i]
            if (matchedPart == "*") {
                val splatParam = StringBuilder(request[i])
                if (numRequestParts != numHandlerParts && i == numHandlerParts - 1) {
                    for (j in i + 1..numRequestParts - 1) {
                        splatParam.append("/")
                        splatParam.append(request[j])
                    }
                }
                splat.add(urlDecode(splatParam.toString()))
            }
            i++
        }
        return splat
    }

    fun getParams(requestPaths: List<String>, handlerPaths: List<String>): Map<String, String> {
        val params = HashMap<String, String>()
        var i = 0
        while (i < requestPaths.size && i < handlerPaths.size) {
            val matchedPart = handlerPaths[i]
            if (matchedPart.startsWith(":")) {
                params[matchedPart.toLowerCase()] = urlDecode(requestPaths[i])
            }
            i++
        }
        return params
    }

    fun urlDecode(s: String): String {
        try {
            return URLDecoder.decode(s.replace("+", "%2B"), "UTF-8").replace("%2B", "+")
        } catch (ignored: UnsupportedEncodingException) {
            return ""
        }
    }

    fun byteArrayToString(bytes: ByteArray, encoding: String?): String {
        var string: String
        if (encoding != null && Charset.isSupported(encoding)) {
            try {
                string = String(bytes, Charset.forName(encoding))
            } catch (e: UnsupportedEncodingException) {
                string = String(bytes)
            }
        } else {
            string = String(bytes)
        }
        return string
    }

    @Throws(IOException::class)
    fun toByteArray(input: InputStream): ByteArray {
        val baos = ByteArrayOutputStream()
        val byteBuffer = ByteArray(1024)
        var b = input.read(byteBuffer)
        while (b != -1) {
            baos.write(byteBuffer, 0, b)
            b = input.read(byteBuffer)
        }
        return baos.toByteArray()
    }
}

{'fraction_non_alphanumeric': 0.10832399103139013, 'fraction_numerical': 0.005605381165919282, 'mean_word_length': 2.0789473684210527, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 25, 'https://': 4, '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': '4885802', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1923, 'n_words': 432}