C# - catch http request and send cached file from shared storage - c#

we want to make a file cache for local internet provider (like squid).
Is it possible in C# to catch request for a file from user and send him local file instead of downloading it from remote server?
Thanks a lot!

Um, it depends upon what you mean by 'catch a request'. You're not talking about squid, are you? If so, problem solved :-).
Otherwise, if you are talking about catching requests that go through a specific web page, you could do it, but it would be pretty hairy. The idea would be to write an http handler that parses a webpage before it is served looking for links. If it finds one and has a local cache, you would re-write the link to point to your copy.
The other (um, better) alternative would be to parse for links when the pages are being created, which would save you a lot of processing power as the pages are being served. each link could be rewritten to pass through a proxy process that caches files and routes based upon its existence.
I told you it was hairy :-)

Related

Calling a (404) inexisting page Vs blank page. Which one consumes less resources?

Recently I've made an application in which I've made a stupid mistake by hard cording the url I want to post data to instead of using a proxy such as no-ip.
In short words, this app is sending requests regularly to my site which ended up in consuming lots of resources to my site. The request are sent to a PHP page which doesn't exist on my site:
http://www.example.com/non-existing-page.php
I suspect that it's impossible to prevent the distributed app from sending requests to my site without changing the url of my website.
The thing is that I cannot change the url and transfer my site to different URL, So what I need to know now is what should I do in order to make this stupid mistake less resource consuming.
1- Keep things as it is
or
2- Creating a blank php with the name of the called script..
Here is the short question:
When I call a page on a remote server through WebClient, which thing consume more server resources, calling a blank php page or calling an inexisting php page..
Thanks in advance
In a general broad sense whether the 404 page will be displayed or not depends on the browser.
IE doesn't show a custom 404 page unless it's larger than 512 bytes.
See here
Same deal with chrome too.
If you do want to put a custom 404 page make sure you include a favicon in it. Otherwise it leads to really long loading times. Discussed at length here

Can an ASP web app write to a log file on end user computer

Ok I know this is a probably a very basic question but I'm more of a winform person.
Question is simple.
If we Add System.IO to a Web Form . Can we then use a StreamWriter to write a log file to the end user Computer?
For example if page_Load use the following code:
StreamWriter sr=New StreamWriter("C:\abc.log)
sr.Write("ABC")
Then where this abc.log file is created? on webserver or on end user? If on websever then how can we write it on the end user machine?
I want to write a log file on the client machine. What are options? Is using a cookie an option?
No - that is not the way the web works...
You could present the Log as an download, that would be the cleanest solution.
As everyone in this thread has pointed out: this is simply not possible. Now depending on what you are trying to store you could leverage cookies to store some information on the users computer but this can be cumbersome and you are limited to 4K of information. That being said HTML5 offers the new JavaScript objects localStorage and sessionStorage which are basically a key/value dictionaries. localStorage would probably suit your needs best I think since it will persist information on the users PC after the browser is closed. For example:
function logSomeData(message) {
localStorage.logFile += message;
}
function showData() {
alert(localStorage.logFile);
}
If you run the logSomeData function in a browser, close the browser, then come back and run the showData() function the value you added to logFile will be persisted. This isn't a new concept, this is doing exactly what cookies do today except that localStorage is easier to work with (in my opinion) and it can store much more information (2MB to 10MB depending on the browser). It is worth pointing out that this is a new technology so older browsers like IE7 can't use localStorage.
Please let me know if you have any other questions!
As far my knowledge you can't write the client file using asp.net.
In your example you are writing the file where application is hosted.
Means the application server's c:\abc.log
All the code behind in asp.net runs on the server so the code you posted will write to the server machine.
Browsers don't allow interaction with the local machine file system for security reasons
You could use some other technology (e.g. an ActiveX control hosted in a web page)
why you want log file on client site ?
if you want to store some information on client site use cookies
If you're stuck to Internet Explorer 6, the only solution to be able to store client-side data, is to use userData Behavior. It well let you store up to 128kb.
I would recommend you to have a look at jStorage. Which is:
a cross-browser key-value store database to store data locally in the
browser - jStorage supports all major browsers, both in desktop (yes -
even Internet Explorer 6) and in mobile.
It's mainly based on HTML5 localstorage but it will switch to any available technology (like userData Behavior) when HTML5 is not supported.
You could download the text file of log to client machine if you flush the .txt file in the respose object i,e (response.write)

Allowing the user to save the file from JavaScript

There are a lot of JavaScript utilities now to allow posting a file in an AJAXified way to the server these days. Is there any utility that allows streaming bytes to the client and download a file? Or dues that still have to be a server-side solution? I'm using .NET.
Thanks.
There's no cross-browser support for accessing the filesystem of the client. You could probably do it with Flash/Java, but a much cleaner solution would be to do it in the server and create a download link for the user.
Brian: What you said about Javascript posting files to a server seems an incomplete statement. Only way javascript can post files to a server is by having the user manually selecting the file he/she wants to upload...
To answer your question...
You should be able to issue some sort of ajax call (to a web service, for example), have the web service read the file into a byte array and return it to the client. On the client side, you would need to assemble the byte array. I would assume you'll have to also set the appropriate response type from the web service call.
This seems to be something like what you are looking for.

Uploading/Pushing Data To A Website

I need to let a company push information up to my site.
The best way to explain what I am talking about is to explain how it is currently done with their previous website:
This company uploads a CSV file to an FTP set up by the website. The website then processes the CSV file and puts it into an SQL database so that it can be used by the website.
In this case, I am the website and I am working with the company. Both sides are willing to change what they do. So my question is...
What is the best way to accept batch information like this? Is there a more automated way that doesn't involve FTP? In the future I may have a lot of companies wanting to do this, and I'd hate to have to setup accounts for each one.
The project is C# ASP.NET MSSQL
Let me know if you need more information...
Set up a web service to accept incoming data. That way you can validate immediately and reject bad data before it ever gets into your system.
If you want to eliminate FTP, you could allow them to upload files to your site leveraging using FileUpload. Once the file is uploaded you can do your server side processing.
EDIT: From the OP's comment's it seems to be an automated process. That said, if their process generates the file, you could:
Allow them to continue their current process which would involve them generating their file and placing it somewhere where it could be accessed via a URI with authentication, you could access this file on a schedule and process it. From what it seems right now they generate a file and upload it to your FTP server, so there seems to a manual element to begin with.

Can I intercept a file upload over HTTP to IIS7 and deny the upload based on a validation criteria?

I have a form which you can post a file over to my server.
Is there a way, without using a custom HTTP handler, to intercept the start of the post and prevent it before the file is uploaded and bandwidth used up.
So for example, I could check an IP address and if it's blocked in my system I could stop the request before I waste 10MB of uploading.
Thanks!
EDIT:
I'm using ASP.NET MVC
This is really a job for a HttpModule, not a HttpHandler. Just check remote IP against a blacklist in the BeginRequest event and null route or what have you.
As far as I am aware, this isn't possible with stock .NET code, unless you spend time to recreate the file handling and parsing ASP.NET does.
In ASP.Net 2.0, a new FileUpload control was added that can be used to limit file types and sizes, among other things.

Categories