Skip to content

Commit

Permalink
fix(core): avoid redirect hell
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed May 30, 2021
1 parent a8d5cdb commit da3df9d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/src/crawler/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default async function login(
// ensure redirection to the right auth service
res = await fetch.get(startPointFinder)
// get basic auth url
const authUrl = fetch.redirectUrl!
const authUrl = fetch.redirectUrl || startPointFinder
// redirect to auth, start login
res = await fetch.follow()

Expand Down
3 changes: 0 additions & 3 deletions core/src/utils/cookie-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export function cookieParse(host: string, headers: Headers): CookieMap {
let [lastIdxMark, kv] = ['', new Map()] as [string, Map<string, string>]
for (const e of rawCookies) {
const [keyVal, ...optionals] = e.split('; ')
const [_, path] = optionals
.find((value) => value.match(/path/i))!
.split('=')
if (!keyVal) {
continue
}
Expand Down
5 changes: 4 additions & 1 deletion core/src/utils/fetch-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class FetchWithCookie {
private headers: StringKV
private cookieMap?: CookieMap
private lastRes?: Response
private lastRedirectUrl?: string
redirectUrl?: string
constructor(headers: StringKV) {
this.headers = headers
Expand All @@ -27,7 +28,8 @@ export class FetchWithCookie {
*/
async follow(options?: FetchCookieOptions): Promise<Response> {
let res: Response
if (this.redirectUrl) {
// avoid callback hell
if (this.redirectUrl && this.lastRedirectUrl !== this.redirectUrl) {
res = await this.fetch(this.redirectUrl, options || {})
await this.follow(options || {})
} else {
Expand Down Expand Up @@ -62,6 +64,7 @@ export class FetchWithCookie {
redirect: 'manual',
}).catch(console.error)) as Response

this.lastRedirectUrl = this.redirectUrl
this.redirectUrl = res.headers.get('location') || undefined
this.lastRes = res
this.updateMap(cookieParse(host, res.headers))
Expand Down
1 change: 0 additions & 1 deletion plugins/check-in/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ export class CheckIn {

export async function checkIn() {
// Get cookie
// `https://sec.whpu.edu.cn/rump_frontend/login/?next=${encodeURIComponent('https://jwglxt.whpu.edu.cn/sso/jziotlogin')}`
await handleCookie()
// Log in and save cookie to cea
const users = sstore.get('users')
Expand Down

0 comments on commit da3df9d

Please sign in to comment.