How to distinguish between a data uri and an image url? - regex

I have a php script that handles the url sent to it via ajax from a js file. This is either a data uri or an image url. What would be the best way to distinguish whether the string supplied is a data uri or an image url, from the php script. I was thinking of using a regex to test the data uri. But i dont seem to come with a right regex that can handle the data uri.

Well... if it starts with data: then it's a data URL. So... if( substr($url,0,5) == "data:") should do it.
Remember: KISS.

Check the extension of the URL.
Regex: \.[^\.]*$

Related

How to parse multipart/form-data forwarded by Mailgun API on AWS Lambda?

Here is what I am trying to achieve: Users will be able to email me at test#myDomain.com which is integrated with the Mailgun service. Based off a rule I'll have set Mailgun will forward this email as a POST request to https:url-to-my-lambda-function.amazonaws.com/email. I then receive this POST request in Lambda, parse it, then grab and use the html from the email for further work.
The issue I am running into is that Mailgun sends their POSTs not as JSON but as a multipart/form-data. I have not been able to find a way to parse this content type and grab the email html. Here is a sample forwarded email from Mailgun (note that technically mailgun sends along attachments too, but all I care about is the stripped-html field).
Also note that I am working with the Serverless framework and need to write this function in either Python or Node.js.
Things I have tried to do to parse the POST request:
Tried using the parse-multipart npm library, but was unable to get it to work I think because I couldn't find the boundary string. This might still be a tenable solution.
Tried let body = new String(decodeURIComponent(event.body)); , to put the data into a string and decode it because it seemed to be coming in URL encoded. Then I was using regex to find and pull out the stripped-html that I wanted. However, I think the URL decoding wasn't properly working because it was converting spaces into pluses. See the block below as an example of what I got:
<td+align="center"+valign="top"+id="m_-6910385412628668961m_-3845437051063103019m_-3682438291175620773bodyCell"+style="padding-bottom:40px;height:100%;margin:0;padding:0;width:100%;border-top:0">\n++++++++++++++++++++++++\n++++++++++++++++++++++++<table+border="0"+cellpadding="0"+cellspacing="0"+width="100%"+style="border-collapse:collapse">\n++++++++++++++++++++++++++++<tbody><tr>\n++++++++++++++++++++++++++++++++<td+align="center"+valign="top">\n++++++++++++++++++++++++++++++++++++\n++++++++++++++++++++++++++++++++++++<table+border="0"+cellpadding="0"+cellspacing="0"+width="100%"+id="m_-6910385412628668961m_-3845437051063103019m_-3682438291175620773templatePreheader"+style="border-collapse:collapse;background-color:#eaeaea;border-top:0;border-bottom:1px+solid+#d0d0d0">\n++++++++++++++++++++++++++++++++++++++++<tbody><tr>\n++++++++++++++++++++++++++++++++++++++++\t<td+align="center"+valign="top">\n++++++++++++++++++++++++++++++++++++++++++++++++<table+border="0"+cellpadding="0"+cellspacing="0"+width="600"+style="border-collapse:collapse">
I have also tried using libraries like busboy to decode the multipart/form-data but have had issues with getting them working in Lambda because they work off of requests.
How can I parse this POST request? Is this even possible?
You should probably consider using a module designed to parse multipart form data such as https://www.npmjs.com/package/parse-formdata.

how to get the value of special character like #,?,;

I am using Advanced Rest Client plugin in Google Chrome.
URL: http://localhost:6721/bgs/ujh/data# or data? or data;
method: GET
When I pass this data in my controller I am getting data only instead of data# or data? or data;.
You should urlencode the data. # for example would become %23
http://localhost:6721/bgs/ujh/data%23
The # character indicates a fragment. It is not sent as part of a HTTP request. It is only used within the browser to navigate to a part of a page. That's why you are only seeing data; the fragment component of the URL is being stripped.

How to send Queryparam with JSON value using Jersey client in unit test

I am trying to write a test case for a jersey resource using InMemory container provided by Jersey.
As my service method contains many multivalued parameters as filters, I opted to send all of those values as single JSON parameter, so that it will be easy to send a list of values for each filter.
When i send the JSON string using target("path").queryParam("filters", jsonString).request().get(); the call fails die to Jersey clients internal query builder, which is parsing the url and checking for path param templates in the url. Since the url contains my JSON with "{" in it, they are interpreted as path param.
If I try to encode the JSON using URLEncode.encode(jsonStr, "UTF-8"), the path param template issue is solved but white spaces in JSON are received by server as "+" as jersey client encoding URL one more time, but server decoding it only once.
If I make the Queryparam as post param test is working, but i don't want to use POST for just to retrieve data.
I can't post original code due to company policies.
My question is, is there any way to disable path template check in jersey clieny by setting custom builder.
A simpler solution would be to replace the '+' by '%20' as suggested here and here:
URLEncode.encode(jsonStr, "UTF-8").replaceAll("\\+", "%20");

How to convert url to regex statement

I want to use ACL for authorization (https://www.npmjs.com/package/acl) but i can't use it with complex urls like http://domain.com/api/v1/resource/:id1/anotherResource/:id2
Using node js, express can handle finding right function from the url from request's originalUrl or something else. I need to put regular expressions statements of requested urls into configuration of ACL.
I debugged express step by step and found regex form of url on 84th line of Layer.js but I need to know every regex form of url to use ACL module as a middleware.
Is there any way to get regexp string (like in the picture below) of url to use in middleware?
Use path-to-regexp module which is used internally by expressjs (actually you were very close to finding it while debugging)

django and angular url patterns

Sometimes in my code I pass get parameters with URL's. One particular scenario is if the user is not logged in, but puts a URL for a page that requires login, they will be required to login first.
In that case I may have a URL such as: www.example.com/home/#/main/.
The end of the URL /#/main/ is for angular. However, in django when I do the below to get the next parameter above, I do this:
self.request.GET.get('next', self.redirect_url)
The problem is that in this case, next provides everything but the angular portion, so I get: www.example.com/home/.
Is there anyway to get the remaining portion of the URL as well?
You have to urlencode the url before you add it as a parameter. Then it will turn into %23 and insn't the separator for the anchor anymore, which is handled client side only as KVISH described.
Apparantly you can't. Django doesn't even see the anchor, its all handled on client (browser).
How to identify an anchor in a url in Django?
The way I got around this is I use jQuery to set a hidden input field to the hash location, which can be obtained like so:
window.location.hash
The hash gets submitted with the form and I can take it from there.