The Weird and The Wonderful
The Weird and The Wonderful forum is a place to post Coding Horrors,
Worst Practices, and the occasional flash of brilliance.
We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid
workarounds and developers just not quite getting it. And then somedays we come across - or write -
the truly sublime.
Post your Best, your worst, and your most interesting. But please - no
programming questions . This forum is purely for amusement and discussions on code snippets. All
actual programming questions will be removed.
|
|
I am doing a lot of computational maths work at home at the moment and, just for giggle, I thought of writing the following method. Code behave as incorrectly as expected!
static void Main(string[] args)
{
var e = 1e18;
Console.WriteLine(IsEven(e));
Console.WriteLine(IsEven(e + 1));
Console.WriteLine(IsEven(-e));
Console.WriteLine(IsEven(-e + 1));
}
bool IsEven(double d)
{
if (d < 0) return (d % 2) > -1;
return (d % 2) < 1;
}
|
|
|
|
|
What makes this even nicer is that both 1.0e+18 and 1.0 are exactly representable as a double, but their sum isn't. A nice demonstration of one of the pitfalls of floating-point arithmetic.
Ad astra - both ways!
|
|
|
|
|
ha, yeah, it's better than summing up 0.1 10 times, since 0.1 is an approximation to start with...
|
|
|
|
|
First of all, the code is super insecure.
This code allows anyone to hit a URL on my web site and create a user directory and one html file (which I fill in with their user name).
** EDIT **
I've implemented SSL on the site (as of 2018-04-25) so you can use the HTTPS version to try it if you like:
https://newlibre.com[^]
** END EDIT **
Usage
I've posted this at my web site : NewLibre.com[^]
To try it out you just hit : http://newlibre.com/api/values/<username> where <username> is a string representing the user name you'd like to create. It cannot include characters that don't work in a directory (because it creates a directory based upon your user name).
Sample
Here's a sample you can try : http://newlibre.com/api/values/superstar
If someone gets there before you, it simply notices that the directory and file is already created and redirects you there.
What Happens
When you navigate to the URL (get) the system:
1. creates a directory under my web site named after your username
2. generates a default html file with your user name in it
3. redirects you to the newly created page and loads it.
I'm Being Absurd
Isn't this absurd!?! I'm letting you navigate to a URL and generate a new folder and html page on my web server.
I know this opens me up for everyone create a new folder and just being mean-spirited but this is a prototype and I'm amazed at how simple the code is.
Also, they are created in a subdir and I will just delete the subdir.
I hope you find this interesting (and Weird & Wonderful).
This Is all The code it takes to do that
[HttpGet("{userName}")]
public HttpResponse Get(string userName)
{
var currentDir = Directory.GetCurrentDirectory();
DirectoryInfo di = Directory.CreateDirectory(Path.Combine(currentDir,@"wwwroot\allUsers\",userName));
string allHtml = $"<!doctype html><html><head><title>newlibre\\{userName}</title></head><body>newlibre\\{userName}</body></html>";
string htmlFileName = Path.Combine(di.FullName, $"{userName}.htm");
if (!System.IO.File.Exists(htmlFileName))
{
System.IO.File.AppendAllText(htmlFileName, allHtml);
}
var newUrl = "http://" + HttpContext.Request.Host + "/allUsers/" + userName + "/" + $"{userName}.htm";
Response.Redirect(newUrl);
return Response;
}
EDIT
I've altered the default page which is generated when you enter your userName, so it now looks like :
https://i.stack.imgur.com/rXxXB.png^
The new template that is created loads the daily dilbert strip!!
I hope you find that quite cool.
Also, if you type a dilbert.com daily strip link and click the button it will get that one and display it on your page (ie link will be like http://dilbert.com/2018-04-01)
New Content Each Day
Also, when you visit your page each day the newest dilbert will be displayed.
Although depending upon browser, it may take a manual refresh.
EDIT 2 - New Feature
I've now made it possible to generate your new (sub) web site based upon your user name by :
1) entering your user name in the text box
2) pressing the [Create UserName] button.
I simply added it at the top of the default page (which is ugly)
https://i.stack.imgur.com/zrgDW.png[^]
So now you can just go to the main web site (NewLibre.com[^]) and try it out.
I Know
This is getting ridiculous and needs to be an article. This is the prototype to something much larger.
EDIT 3 - More features
I now add a little message that the home page is created for the new user.
It looks like :
https://i.stack.imgur.com/Ivwr1.png[^]
I'll stop now.
I'm far enough with the prototype that it shows me what I can do.
More to come with a real article soon (hopefully).
modified 8hrs 20mins ago.
|
|
|
|
|
You might want to check for invalid file name characters in the username. At the moment, I suspect it might be possible to use some version of "..\" to put files in a parent folder.
I've tried a couple of versions which don't seem to have worked. But if you see a "test" folder and "test.htm" file in your "wwwroot" folder, then it's trivial to hack.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I looked and I don't see any new ones, but I will definitely implement some checks against that.
Thanks for mentioning it.
I was able to create one with spaces in it like:
http://newlibre.com/api/values/this is a good test
Wow.
|
|
|
|
|
It works ! but I would like a funny picture in the created website 
|
|
|
|
|
I've implemented your idea. A funny pic...ala the daily dilbert.com
your page will now look like:
https://i.stack.imgur.com/rXxXB.png^
The new template that is created loads the daily dilbert strip!!
I hope you find that quite cool.
Also, if you type a dilbert.com daily strip link and click the button it will get that one and display it on your page (ie link will be like http://dilbert.com/2018-04-01)
I've deleted the old user directories so try creating yours again and you will see this.

|
|
|
|
|
|
That was cool you tried it. Thanks.
It's just some funny code I've been thinking of for a long while and the dotnet core stuff really does make this easier. Along with Visual Studio and the built-in deployment and my web host (smarterasp.net) it's pretty amazing.
|
|
|
|
|
I have not yet done much with .NET Core, but it has my attention.
Recently I found out how to build (publish) and version .NET Core projects in a simple way on our builder as my colleagues had made some projects, took me some time to figure that out
If you are interested, there's a tip about the versioning trick ...
|
|
|
|
|
RickZeeland wrote: there's a tip about the versioning trick
I took a look at that. That's an interesting idea. Thanks for writing it up. 
|
|
|
|
|
Hey, I deleted your folder/web again so you can try it again.
Now it:
creates the user's page named as index.htm so the user will have a shorter URL to get to for their web page (http://newlibre.com/allUsers/RickZeeland) will work when you create it again.
Also, you can try creating it using the text box and button on the main page (newlibre.com[^].
If you get a chance. Thanks.
|
|
|
|
|
|
RickZeeland wrote: Nice landing page,
Thanks and thanks for trying it out again. 
|
|
|
|
|
Here's a tulip for you which I photographed today: OneDrive - Tulips.[^]
You have my permission to publish it on your website 
|
|
|
|
|
From a GitHub hosted piece of OSS ...
string key = @"
-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
|
and people think online banking is safe because "they say so."
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
You can find a surprising number of application authentication keys for various services rummaging through GitHub as well.
|
|
|
|
|
Click here[^] for more information.
/ravi
|
|
|
|
|
Someone needs a whack with a clue bat.
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
From the docs for Image.FromFile
Quote: Exceptions
OutOfMemoryException
The file does not have a valid image format.
-or-
GDI+ does not support the pixel format of the file.
...and using something like UnsupportedFormatException is too hard?
cheers
Chris Maunder
|
|
|
|
|
... because they thrash round chewing up memory trying to find a valid format?
Wouldn't surprise me in the least.
[edit] Removed joke icon in light of other messages in this thread. [/edit]
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
modified 25-Mar-18 22:09pm.
|
|
|
|
|
Pretty much my assumption. "Let's try it and see what breaks".
cheers
Chris Maunder
|
|
|
|
|
|
|
General
News
Suggestion
Question
Bug
Answer
Joke
Praise
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.