From c7300c343cca9b22c9c5bdb64823e312dac1e495 Mon Sep 17 00:00:00 2001 From: "Yuan (Bob) Gong" Date: Fri, 24 Jul 2020 12:16:22 +0800 Subject: [PATCH] fix: all big enough ui proxy requests fail with error proxying with partial data (#4266) * fix: all big enough ui proxy requests fail with error proxying with partial data * Fix naming --- frontend/server/app.ts | 4 ++++ frontend/server/consts.ts | 7 +++++++ frontend/server/handlers/artifacts.ts | 2 ++ frontend/server/proxy-middleware.ts | 4 ++-- 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 frontend/server/consts.ts diff --git a/frontend/server/app.ts b/frontend/server/app.ts index 3d7d1da7f6c..0664030e666 100644 --- a/frontend/server/app.ts +++ b/frontend/server/app.ts @@ -34,6 +34,7 @@ import { getIndexHTMLHandler } from './handlers/index-html'; import proxyMiddleware from './proxy-middleware'; import { Server } from 'http'; +import { HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS } from './consts'; function getRegisterHandler(app: Application, basePath: string) { return ( @@ -163,6 +164,7 @@ function createUIServer(options: UIConfigs) { onProxyReq: proxyReq => { console.log('Metadata proxied request: ', (proxyReq as any).path); }, + headers: HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS, target: envoyServiceAddress, }), ); @@ -193,6 +195,7 @@ function createUIServer(options: UIConfigs) { onProxyReq: proxyReq => { console.log('Proxied request: ', proxyReq.path); }, + headers: HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS, target: apiServerAddress, }), ); @@ -203,6 +206,7 @@ function createUIServer(options: UIConfigs) { onProxyReq: proxyReq => { console.log('Proxied request: ', proxyReq.path); }, + headers: HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS, pathRewrite: pathStr => pathStr.startsWith(basePath) ? pathStr.substr(basePath.length, pathStr.length) : pathStr, target: apiServerAddress, diff --git a/frontend/server/consts.ts b/frontend/server/consts.ts new file mode 100644 index 00000000000..9eea2b55290 --- /dev/null +++ b/frontend/server/consts.ts @@ -0,0 +1,7 @@ +// The hack solution I found for solving: +// Responses fail randomly half way in a partial state, with the last sentence +// being: "Error occurred while trying to proxy request ..." +// Reference: https://github.com/chimurai/http-proxy-middleware/issues/171#issuecomment-439020716 +export const HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS = { + Connection: 'keep-alive', +}; diff --git a/frontend/server/handlers/artifacts.ts b/frontend/server/handlers/artifacts.ts index 92cb4b2b672..cc647f0c3c7 100644 --- a/frontend/server/handlers/artifacts.ts +++ b/frontend/server/handlers/artifacts.ts @@ -19,6 +19,7 @@ import { createMinioClient, getObjectStream } from '../minio-helper'; import { Handler, Request, Response } from 'express'; import { Storage } from '@google-cloud/storage'; import proxy from 'http-proxy-middleware'; +import { HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS } from '../consts'; /** * ArtifactsQueryStrings describes the expected query strings key value pairs @@ -299,6 +300,7 @@ export function getArtifactsProxyHandler({ return namespacedServiceGetter(namespace); }, target: '/artifacts/get', + headers: HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS, }, ); } diff --git a/frontend/server/proxy-middleware.ts b/frontend/server/proxy-middleware.ts index 98b9624a47c..cdc1ea38ebb 100644 --- a/frontend/server/proxy-middleware.ts +++ b/frontend/server/proxy-middleware.ts @@ -15,6 +15,7 @@ import express from 'express'; import proxy from 'http-proxy-middleware'; import { URL, URLSearchParams } from 'url'; +import { HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS } from './consts'; export function _extractUrlFromReferer(proxyPrefix: string, referer = ''): string { const index = referer.indexOf(proxyPrefix); @@ -72,14 +73,13 @@ export default (app: express.Application, apisPrefix: string) => { changeOrigin: true, logLevel: process.env.NODE_ENV === 'test' ? 'warn' : 'debug', target: 'http://127.0.0.1', - router: (req: any) => { return _routePathWithReferer(proxyPrefix, req.path, req.headers.referer as string); }, - pathRewrite: (_: any, req: any) => { return _rewritePath(proxyPrefix, req.path, req.query); }, + headers: HACK_FIX_HPM_PARTIAL_RESPONSE_HEADERS, }), ); };