Writing a custom data protocol handler to download .ICS file

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Writing a custom data protocol handler to download .ICS file

Postby bgiroux » Wed Mar 25, 2020 1:19 pm

Hi everyone,

I have a requirement for an application that I'm supporting to be able to parse the following URI.

data:text/calendar;charset=utf8,BEGIN:VCALENDAR[calendarContent]END:VCALENDAR

The calendar content I've indicated here is a serialized representation of what an ICS (calendar file) would contain - I'm omitting it for brevity/confidentiality.

For this, I've created a custom protocol handler for that data: protocol. I've confirmed that my ProcessRequestAsync() is correctly intercepting the request and saving the necessary information as a stream.

However, I'm at a loss for how to correctly handle the downloading of the ICS stream to a downloads folder (similar to what Chrome would do in this instance). Here is what I have so far:

Code: Select all
            public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback)
            {
                Task.Run(() =>
                {
                    using (callback)
                    {
                        Stream stream = null;

                        string vcal =
                            request.Url.Substring(request.Url.IndexOf("BEGIN:VCALENDAR"),
                            request.Url.Length - request.Url.IndexOf("BEGIN:VCALENDAR"));
                       
                        stream = GetMemoryStream(Uri.UnescapeDataString(vcal), Encoding.UTF8);

                        if (stream == null)
                        {
                            callback.Cancel();
                        }
                        else
                        {
                            stream.Position = 0;
                            ResponseLength = stream.Length;
                            MimeType = "text/calendar;charset=utf8";
                            StatusCode = (int)HttpStatusCode.OK;
                            Stream = stream;
                           
                            callback.Continue();
                        }
                    }
                });

                return CefReturnValue.ContinueAsync;
            }


When I use a FileStream from the System.IO namespace, I can save it to my hard disk at the specified location, but I wonder if there's a better way to do this? I'm aiming for the cleanest approach possible that would be behave as closely as possible to how Chrome would handle this.
bgiroux
Newbie
 
Posts: 1
Joined: Wed Mar 25, 2020 12:47 pm

Re: Writing a custom data protocol handler to download .ICS

Postby magreenblatt » Wed Mar 25, 2020 1:47 pm

Since you're using a data URI all of the information is included in that URI string. I suggest that you parse the string in a method like OnBeforeBrowse or OnBeforeResourceLoad and use platform API functions to save the contents on disk.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 45 guests