/** * Makes sure the last character is a slash. * * @param path * @return */ public String getNormalizedDirectoryPath(final String path) { if (isDirectoryPath(path)) { return path; } return path + SLASH; }
@Test public void shouldNotAcceptInvalidCharacters() { assertThat(pathHelper.isPathContainingIllegalCharacters("somepath../"), is(false)); assertThat(pathHelper.isPathContainingIllegalCharacters("../somepath"), is(true)); assertThat(pathHelper.isPathContainingIllegalCharacters("somepath/../"), is(true)); assertThat(pathHelper.isPathContainingIllegalCharacters(null), is(true)); }
private DirectoryIndexDescriptor loadDirectoryIndexResource(final String path) { String normalizedDirectoryPath = pathHelper.getNormalizedDirectoryPath(path); for (String index : serverConfig.getDirectoryIndex()) { String directoryIndexPath = normalizedDirectoryPath + index; ResourceProvider resourceProvider = getResourceProvider(directoryIndexPath); if (resourceProvider != null) { return new DirectoryIndexDescriptor(resourceProvider, directoryIndexPath); } } return null; }
@Test public void shouldLogIOExceptionsSilently() throws Exception { Socket socket = mock(Socket.class); HttpServletResponseImplFactory responseFactory = mock(HttpServletResponseImplFactory.class); when(responseFactory.createFromSocket(socket)).thenThrow(new IOException()); ServerRunnable serverRunnable = new ServerRunnable(socket, mock(ServerConfig.class), mock(HttpServletRequestImplFactory.class), responseFactory, mock(HttpErrorHandlerResolver.class), new PathHelper() ); serverRunnable.run(); verify(socket, times(1)).close(); } }
if (pathHelper.isPathContainingIllegalCharacters(requestedPath)) { throw new AccessDeniedException();
@Test public void shouldNormalizeDirectoryPath() { assertThat(pathHelper.getNormalizedDirectoryPath("somepath"), is("somepath/")); assertThat(pathHelper.getNormalizedDirectoryPath("somepath/"), is("somepath/")); }
public ServiceContainer(final ServerConfig serverConfig) { HeadersParser headersParser = new HeadersParser(); requestWrapperFactory = new HttpServletRequestImplFactory(headersParser, new QueryStringParser(), new RequestStatusParser(), new CookieParser(), new MultipartHeadersPartParser(headersParser), serverConfig.getTempPath() ); responseFactory = new HttpServletResponseImplFactory( new HeadersSerializer(), new CookieHeaderSerializer(new DateProvider()), new StreamHelper( new RangeHelper(), new RangePartHeaderSerializer() ) ); threadPoolExecutor = new ThreadPoolExecutor(1, serverConfig.getMaxServerThreads(), DEFAULT_TIME_UNITS, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(serverConfig.getMaxServerThreads() * MAX_THREADS_MULTIPLIER), Executors.defaultThreadFactory(), new ServiceUnavailableHandler(responseFactory) ); httpErrorHandlerResolver = new HttpErrorHandlerResolverImpl(serverConfig); pathHelper = new PathHelper(); }
@Test public void shouldSayWhetherDirectoryPath() { assertThat(pathHelper.isDirectoryPath("somepath"), is(false)); assertThat(pathHelper.isDirectoryPath("somepath/"), is(true)); } }
private void handleDirectoryIndex(final HttpServletResponseImpl response, final HttpServletRequestImpl request, final String requestedPath) throws IOException { DirectoryIndexDescriptor indexDescriptor = loadDirectoryIndexResource(requestedPath); if (indexDescriptor == null) { throw new NotFoundException(); } else { if (!pathHelper.isDirectoryPath(requestedPath)) { sendRedirectToDirectorySlashedPath(response, requestedPath); } else { indexDescriptor.getResourceProvider().load( indexDescriptor.getDirectoryPath(), request, response); } } }