Skip to content

Commit

Permalink
Merge pull request #22 from Wyn-Price/dev/no-prob-chars
Browse files Browse the repository at this point in the history
Remove problematic char checking
  • Loading branch information
Wyn-Price authored Dec 3, 2023
2 parents e185556 + be6f2f3 commit c64706d
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/normaljar.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { RequestHandler } from "express";
import { fetchDownloadUrl, getFetchedData, getRedirectUrl } from "./util";
import { fetchDownloadUrl, getFetchedData } from "./util";

const normaljar: RequestHandler = async (req, res) => {
const { id, file } = res.locals

const response = await fetchDownloadUrl(id, file)
if (response.ok) {
return res.redirect(getRedirectUrl(await getFetchedData(response)))
return res.redirect(await getFetchedData(response))
} else {
return res.status(response.status).send(response.statusText)
}
Expand Down
2 changes: 1 addition & 1 deletion src/testing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import escapeHTML from 'escape-html';
import { RequestHandler } from 'express';
import createClassifierMap from './classifiermap';
import { authFetch, getDownloadUrl, getFetchedData, getRedirectUrl } from './util';
import { authFetch, getDownloadUrl, getFetchedData } from './util';


const testing: RequestHandler = async (req, res) => {
Expand Down
37 changes: 0 additions & 37 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,3 @@ export const getFetchedData = async (response: Response) => {
const json = await response.json()
return json.data
}

//Gets the redirect url for the given url. An example of this download url would be: https://edge.forgecdn.net/files/2724/420/jei_1.12.2-4.15.0.281.jar
//
//Due to an issue with apache's http client which gradle uses, some characters will be decoded, but not encoded.
//This in junction with curseforge's media server needing the correct encoding means redirecting to the media server won't always work.
//A fix for this is to redirect to `/download-binary/...`, instead of `https://media.forgecdn.net/files/...`. Doing this means I have to encode the
//Jar name twice (hopfully) meaning that the apache issues don't occur, as the special characters won't show up in the decoding.
//The `/download-binary/` is just a reverse proxy to the forge media server, however I'm not too sure that it doesn't use up bandwidth when downloading the jar, so I only use this when I need to.
//
//If the file name (jei_1.12.2-4.15.0.281.jar) contains any problematic characters then return a redirect to `/download-binary/`, otherwise it redirects to curseforge's media server.
//
//
//2022-01-02T06:14:58.021+0000 [DEBUG] [org.gradle.internal.resource.transport.http.HttpClientConfigurer$DowngradeProtectingRedirectStrategy] Redirect requested to location 'https://media.forgecdn.net/files/3335/93/BetterFoliage-2.6.5%2B368b50a-Fabric-1.16.5.jar'
//2022-01-02T06:14:58.022+0000 [DEBUG] [org.apache.http.impl.execchain.RedirectExec] Resetting target auth state
//2022-01-02T06:14:58.022+0000 [DEBUG] [org.apache.http.impl.execchain.RedirectExec] Redirecting to 'https://media.forgecdn.net/files/3335/93/BetterFoliage-2.6.5+368b50a-Fabric-1.16.5.jar' via {s}->https://media.forgecdn.net:443
//
//Decoding is done at URLEncodedUtils#urlDecode
//Call stack is below (line numbers are the line of the next call):
//DefaultRedirectStrategy#getLocationURI L 97
//URIUtils#normalizeSyntax L 196
//new URIBuilder --> URIBuilder#digestURI --> URIBuilder#parsePath L 157
//URLEncodedUtils#parsePathSegments L 236
//URLEncodedUtils#urlDecode
export const getRedirectUrl = (url: string) => {

//The file name will be the last one. The reason I pop to get the raw file name instead of just doing `split[6]`, is as `split` is joined back if there are no problematic chars.
var split = url.split('\/')
var rawFileName = split.pop()
var fileName = encodeURIComponent(rawFileName)

// If there are problematic chars then redirect internally.
if (rawFileName.includes("+")) {
return `/download-binary/${split[4]}/${split[5]}/${encodeURIComponent(fileName)}` //We have to encode it twice for this to work with gradle
} else {
return split.join('/') + '/' + fileName
}
}
20 changes: 0 additions & 20 deletions tests/download.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ describe('Normal Download URL', () => {
expect(res.status).toStrictEqual(302)
expect(res.headers['location']).toStrictEqual("https://edge.forgecdn.net/files/2724/420/jei_1.12.2-4.15.0.281.jar")
})

test('Normal Jar w/ problamatic chars should be correct', async () => {
//curse.maven:better-foliage-228529:3335093
const res = await requestWithSupertest.get(downloadUrl('better-foliage', '228529', '3335093', '.jar'))
expect(res.status).toStrictEqual(302)
expect(res.headers['location']).toStrictEqual("/download-binary/3335/93/BetterFoliage-2.6.5%252B368b50a-Fabric-1.16.5.jar")
})

test("Normal Jar that doesn't exist should return 404", async () => {
//curse.maven:invalid-12345:12345
const res = await requestWithSupertest.get(downloadUrl('invalid', '12345', '54321', '.jar'))
expect(res.status).toStrictEqual(404)
})
})

describe('Direct Download URL', () => {
Expand Down Expand Up @@ -67,13 +54,6 @@ describe('Classifier Download URL', () => {
expect(res.headers['location']).toStrictEqual("https://edge.forgecdn.net/files/2452/538/JustEnoughResources-1.12-0.8.2.20-api.jar")
})

test('Classifier Jar w/ problamatic chars should be correct', async () => {
//curse.maven:pehkui-319596:3577084-sources-dev-3577085:sources-dev
const res = await requestWithSupertest.get(downloadUrl('pehkui', '319596', '3577084-sources-dev-3577085', '-sources-dev.jar'))
expect(res.status).toStrictEqual(302)
expect(res.headers['location']).toStrictEqual("/download-binary/3577/85/Pehkui-3.1.0%252B1.18.1-forge-sources-dev.jar")
})

test("Classifier Jar where original jar doesn't exist should return 404", async () => {
//curse.maven:invalid-12345:54321-sources-54322:sources
const res = await requestWithSupertest.get(downloadUrl('invalid', '12345', '54321', '-sources.jar'))
Expand Down
4 changes: 0 additions & 4 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
"source": "/curse/maven/(.+)",
"destination": "/src/app.ts"
},
{
"source": "/download-binary/(.+)%25(2B)(.+)",
"destination": "https://media.forgecdn.net/files/$1%$2$3"
},
{
"source": "/test/(.+)",
"destination": "/src/app.ts"
Expand Down

0 comments on commit c64706d

Please sign in to comment.