Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EPUB3 Bindings #35

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions LauncherOSX/CocoaHTTPServer/Core/Responses/HTTPFileResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ - (NSDictionary *)httpHeaders {
else if([ext isEqualToString:@"css"]) {
return [NSDictionary dictionaryWithObject:@"text/css" forKey:@"Content-Type"];
}
else if([ext isEqualToString:@"xml"]) {
return [NSDictionary dictionaryWithObject:@"application/xml" forKey:@"Content-Type"];
}
else if([ext isEqualToString:@"xhtml"] || [ext isEqualToString:@"html"]) {
return [NSDictionary dictionaryWithObject:@"application/xhtml+xml" forKey:@"Content-Type"];
}
Expand Down
3 changes: 3 additions & 0 deletions LauncherOSX/LOXAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#import "LOXUtil.h"
#import "LOXMediaOverlay.h"
#import "LOXMediaOverlayController.h"
#import "LOXWebViewController.h"

using namespace ePub3;

Expand Down Expand Up @@ -88,6 +89,8 @@ - (id)init

-(void) awakeFromNib
{
[NSURLProtocol registerClass:[Epub3URLProtocol class]];

_epubApi = [[LOXePubSdkApi alloc] init];

self.spineViewController.currentPagesInfo = _currentPagesInfo;
Expand Down
5 changes: 4 additions & 1 deletion LauncherOSX/LOXPackage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ - (RDPackageResource *)resourceAtRelativePath:(NSString *)relativePath {
return nil;
}

NSRange range2 = [relativePath rangeOfString:@"?"];
if (range2.location != NSNotFound) {
relativePath = [relativePath substringToIndex:range2.location];
}
NSRange range = [relativePath rangeOfString:@"#"];

if (range.location != NSNotFound) {
relativePath = [relativePath substringToIndex:range.location];
}
Expand Down
4 changes: 4 additions & 0 deletions LauncherOSX/LOXWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
@class WebView;
@class PackageResourceServer;

@interface Epub3URLProtocol : NSURLProtocol

@end

@interface LOXWebViewController : NSObject<LOXSpineViewControllerDelegate> {

@private
Expand Down
88 changes: 86 additions & 2 deletions LauncherOSX/LOXWebViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,63 @@
#import "PackageResourceServer.h"
#import "RDPackageResource.h"
#import <ePub3/utilities/byte_stream.h>
#import <ePub3/utilities/iri.h>


@interface Epub3URLProtocol ()
@property (nonatomic, strong) NSURLConnection *connection;
+(NSString *)scheme;
@end
@implementation Epub3URLProtocol

static NSString* EPUB3 = [NSString stringWithUTF8String:ePub3::IRI::gEPUBScheme.c_str()];

+(NSString *)scheme;{
return EPUB3;
}

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
NSURL* requestURI = [request URL];
if(NSOrderedSame == [[requestURI scheme] caseInsensitiveCompare:EPUB3]){
return YES;
}

return NO;
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}
- (void)startLoading
{
NSMutableURLRequest *newRequest = [self.request mutableCopy];
self.connection = [NSURLConnection connectionWithRequest:newRequest delegate:self];
}
- (void)stopLoading
{
[self.connection cancel];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.client URLProtocol:self didLoadData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self.client URLProtocol:self didFailWithError:error];
self.connection = nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowed];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self.client URLProtocolDidFinishLoading:self];
self.connection = nil;
}
@end

@interface LOXWebViewController ()

-(void)onPageChanged:(NSNotification*) notification;
Expand Down Expand Up @@ -133,7 +188,6 @@ - (NSURLRequest *)webView:(WebView *)sender
//NSString * prefix2 = [NSString stringWithFormat:@"%@%@", prefix1, folder];
//[path substringFromIndex: [path rangeOfString:prefix1].location]


if (schemeFile != NSOrderedSame && [path hasPrefix:folder]) {
NSString * str = [[NSString stringWithFormat:@"file://%@", path] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
Expand All @@ -144,7 +198,12 @@ - (NSURLRequest *)webView:(WebView *)sender
return newRequest;
}

if (schemeFile != NSOrderedSame)
// ObjectPreprocessor and ContentHandler with epub3:// URI protocol
// See [NSURLProtocol registerClass:[Epub3URLProtocol class]];
//NSString* EPUB3 = [NSString stringWithUTF8String:ePub3::IRI::gEPUBScheme.c_str()];
NSComparisonResult schemeEPUB = [scheme caseInsensitiveCompare: [Epub3URLProtocol scheme]];

if (schemeFile != NSOrderedSame && schemeEPUB != NSOrderedSame)
{
return request;
}
Expand All @@ -154,10 +213,35 @@ - (NSURLRequest *)webView:(WebView *)sender
return request;
}

if (schemeEPUB == NSOrderedSame)
{
NSString* BASE = [NSString stringWithUTF8String:_package.sdkPackage->BasePath().c_str()];
if (![BASE hasPrefix:@"/"]) {
BASE = [NSString stringWithFormat:@"/%@", BASE];
}
if ([path hasPrefix:BASE])
{
path = [path substringFromIndex:[BASE length]];

schemeFile == NSOrderedSame;
}
}

if ([path hasPrefix:@"/"]) {
path = [path substringFromIndex:1];
}

NSString *query = request.URL.query;
if (query != nil)
{
path = [NSString stringWithFormat:@"%@?%@", path, query];
}
NSString *fragment = request.URL.fragment;
if (fragment != nil)
{
path = [NSString stringWithFormat:@"%@#%@", path, fragment];
}

NSString * str = [[NSString stringWithFormat:@"%@/%@/%@", prefix1, _package.packageUUID, path] stringByAddingPercentEscapesUsingEncoding : NSUTF8StringEncoding];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: stringByAddingPercentEscapesUsingEncoding must not be applied to query! (e.g. ampersand character gets escaped twice)

NSURL *url = [NSURL URLWithString:str];

Expand Down
28 changes: 8 additions & 20 deletions LauncherOSX/PackageResourceServer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,11 @@ @implementation PackageResourceConnection

bool isHTML = [ext isEqualToString:@"xhtml"] || [ext isEqualToString:@"html"]; //[path hasSuffix:@".html"] || [path hasSuffix:@".xhtml"];

if([ext isEqualToString:@"svg"]) {
contentType = @"image/svg+xml";
}
else if([ext isEqualToString:@"js"]) {
contentType = @"text/javascript";
}
else if([ext isEqualToString:@"css"]) {
contentType = @"text/css";
if([ext isEqualToString:@"xml"]) {
contentType = @"application/xml"; // FORCE
}
else if(isHTML) {
contentType = @"application/xhtml+xml";
contentType = @"application/xhtml+xml"; // FORCE
}

if (contentType == nil)
Expand Down Expand Up @@ -262,18 +256,12 @@ - (NSDictionary *)httpHeaders {
if(m_resource.relativePath) {

NSString* ext = [[m_resource.relativePath pathExtension] lowercaseString];

if([ext isEqualToString:@"svg"]) {
return [NSDictionary dictionaryWithObject:@"image/svg+xml" forKey:@"Content-Type"];
}
else if([ext isEqualToString:@"js"]) {
return [NSDictionary dictionaryWithObject:@"text/javascript" forKey:@"Content-Type"];
}
else if([ext isEqualToString:@"css"]) {
return [NSDictionary dictionaryWithObject:@"text/css" forKey:@"Content-Type"];

if([ext isEqualToString:@"xhtml"] || [ext isEqualToString:@"html"]) {
return [NSDictionary dictionaryWithObject:@"application/xhtml+xml" forKey:@"Content-Type"]; // FORCE
}
else if([ext isEqualToString:@"xhtml"] || [ext isEqualToString:@"html"]) {
return [NSDictionary dictionaryWithObject:@"application/xhtml+xml" forKey:@"Content-Type"];
else if([ext isEqualToString:@"xml"]) {
return [NSDictionary dictionaryWithObject:@"application/xml" forKey:@"Content-Type"]; // FORCE
}
else
{
Expand Down