Using dropwizard to make RESTful service, the URL can contain a space - java

I have some code making a #Path for an endpoint:
#Path("/productLine:[a-zA-Z]{1,25}}/cat")
I want to allow two word product lines in the URL. I tried this
#Path("productLine:[a-zA-Z ]{1,25}}/cat")
But the client returns a
HTTP 404 Not Found
when I use a request that has two words, like this:
/services/New Host/cat

can u try with:
#Path("productLine:[a-zA-Z\x0B]{1,25}}/cat")

This worked:
#Path("productLine:[a-zA-Z%20]{1,25}}/cat")
The %20 is the HTTP encoding for space.

Related

Jmeter : Issue with HTTP request returns java.net.URISyntaxException

I am using an RSS feed generated by our test application to verify if there are any broken links on our website.
When I use these URLs in the HTTP sampler after some pre-processing (to split the URL to extract protocol, host, domain), most of the URL's are working just fine but I am facing issues in some random cases as explained below.
for example: below mentioned URLs throw URI exception. The separator between the highlighted text in these URLs mentioned below does not seem to a normal hyphen. It's some special character which I guess needs special handling.
Failed URL from the feed
https://abc.xyz.com/article/worksheet-development-planning—manager-preparation-10200/download/
https://abc.xyz.com/article/worksheet-development-planning—worker-preparation-10201/download/
JMeter Result from Assertion Listener
See link below for JMeter Result from Assertion Listener
URL gets encoded with some unexpected characters
You're right, this — symbol needs to be URL-encoded, to wit your URL path should look like
article%252Fworksheet-development-planning%25E2%2580%2594manager-preparation-10200%252Fdownload%252F
JMeter provides __urlencode() function which can perform URL-encoding of the passed parameter on the fly so you can use it directly in the "Path" field of the HTTP Request sampler like:
${__urlencode(/article/worksheet-development-planning—manager-preparation-10200/download/)}
Check out Apache JMeter Functions - An Introduction article to learn more about JMeter Functions concept.

Url encoding issue with Jersey Client

I need to make a service call such as this:
http://myservice.com/path?var1=value1&var2=value2
The issue I have is value1 and value2 ends up getting encoded, and this makes the service call fail. For example, value1 is something like "a=b&b=c;2&&="... it contains special characters, basically.
I am guessing that this is an issue for the service to fix - to properly handle decoding encoded characters, which I do not think it is currently doing.
Here is a sample of how I am making these requests:
WebTarget target = client.target("http://test.com")
.path("path1")
.queryParam("var1", var1);
Builder builder = target.request();
...
What's puzzling to me is that if I make the same request just using Chrome, everything works. So that makes me to believe that I should have some way with the Jersey API of "disabling" the encoding.
Only way I have found so far to use "raw" Url is to use URI.
So call like this
URI uri = URI.create("http://localhost/~Common~0#/edit?vadf&&sfs&&fdsfd=fs&fsd");
WebTarget target = client.target(uri);
You get request url
1 > GET http://localhost/~Common~0#/edit?vadf&&sfs&&fdsfd=fs&fsd
Everything else I tried resulted in encoding special characters.

How to disable curly braces evaluation in URL passed to HttpServletResponse.sendRedirect

I have a URL shortner that should sendRedirect(URL) to URLs specified by users.
Sometimes URL contain curly braces like this: http://example.com?someparam={something}.
Instead of sending response 302 to client browser, my Spring MVC app at Tomcat server gives error 404 with no text.
Apparently it's some sort of URL variable evaluation taking place, can I disable it? I could not find docs regarding this feature.
I know this is an old question but I think the OP was looking for a way to prevent Spring from doing variable replacement in redirect URL
I faced the exact same issue and the fix was using RedirectView
and in RedirectView you can set setExpandUriTemplateVariables(false)
that made it redirect to the url given exactly without Spring trying to replace anything in it
here is how the code looks like
RedirectView redirect = new RedirectView(redirectUrl);
redirect.setExpandUriTemplateVariables(false);
return new ModelAndView(redirect);
Hope that helps
This is not valid Google search URL http://google.com/{something}. It should have been https://www.google.ca/search?q=http{302}
Emphasis is on search?q. After domain name you have specify your service name and then query string if you want to pass some inputs.
When you do http://google.com/{something} then you really do not have any resource or service as {something} so 404 is the expected output.
HTTP 302 is for redirection, I am not sure why you were expecting redirection.
URL encoding will also not help because issue is related to resource/service, if it is not present then you will get 404. URL encoding is not meant to solve problem related to 404.

Anonymize Amazon public URL, decode using a script on Nginx

I have been wondering if its possible to anonymize public URL. When user makes a request with this anonymized public URL, let Nginx decode, fetch and serve the URL.
Example
Public URL http://amazon.server.com/location/file.html
Anonymized URL https://amazon.server.com/09872340-932872389-390643289/983724.html
Nginx decodes 09872340-932872389-390643289/983724.html to location/file.html
Added image below for further clarification. Nginx has a reverse logic to decode, whereas Remote Server has the logic to Anonymize URL.
Question
All I need to know is how would Nginx decode anonymized URL? Nginx got anonymized URL request. There has to be a way to decode it.
This is an answer to the updated question:
Question All I need to know is how would Nginx decode anonymized URL? Nginx got anonymized URL request. There has to be a way to decode it.
Nginx would make a request to a script, e.g., either through proxy_pass or fastcgi_pass et al.
The script could decode the URL and provide the actual URL through a Location HTTP Response Header with a 302 Found HTTP Status.
Nginx would then have the decoded URL stored in the $upstream_http_location variable. It could subsequently be used in another proxy_pass et al within a named location #named, to which you could redirect the processing of the original request from the user through error_page 302 = #named.
In all, each user request would be processed twice within nginx, but it'll all be transparent to the user -- they simply receive the resource through the original URL, with all redirects being done internally within nginx.
Define Anonymize for a URL? You can use any of the same methods as URL shortners such as http://bitly.com. But that is not truely anonymous since there is a definite mapping between the shortened URL and the target public url. If you make this per user based there is still a mapping but it is user based.
Looks like what you are suggesting is a variation on the above scheme where instead of sending the user to the target URL via a redirect you want the your server to actually fetch the content and return to the user. You need to be aware of the linked content in the public URL such as style sheets and images and adjust them accordingly. Many of the standard proxies has this kind of functionality built in. Also take a look at
https://github.com/jenssegers/php-proxy
http://search.cpan.org/~book/HTTP-Proxy-0.304/lib/HTTP/Proxy.pm.
If you are planning to build your own these can serve as a base.
I think what you want to do here is somewhat similar to another question I've answered in the past, where for each request by the client, you effectively want to make two requests to two different upstreams under the hood (first one to an upstream capable of decoding the URL, second one to actually fetch said decoded URL), but, of course, only return one result.
https://serverfault.com/questions/202011/nginx-and-2-upstreams/485044#485044
As mentioned on serverfault, you could use error_page to process another request, after the first one is complete. You could then use $upstream_http_ to make the subsequent request based on the original one, for example, using $upstream_http_location.
You might also want to look into X-Accel-Redirect header, introduced in this context at proxy_ignore_headers.

How to inclide tspecials(<," etc.) in URL for HttpServer?

I have implemented simple com.sun.net.httpserver.HttpServer application using one of examples in the internet.
Server get request as expected except for one exception:
if url contains tspecials: " or '<' "/" request doesn't reach the server at all.
Using simple java.net.ServerSocket all works great and request received where all tspesials are encoded, which is great for me, but i prefer use HttpServer.
for example request: http://127.0.0.1/MyApp/Test?var=<xml id=“1“>value</xml>
works for ServerSocket and not working for HttpServer.
Any help will be appreciated.
You need to encode your urls on the client side, using the % notation:
http://127.0.0.1/MyApp/Test?var=%3Cxml+id%3D%271%27%3Evalue%3C%2Fxml%3E
On the server side, you should use URI.getRawQuery() in case the query contains & characters, and décode parameter values with URLDecoder.decode.

Categories