15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_BASE_MIME_SNIFFER_H__ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_BASE_MIME_SNIFFER_H__ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GURL; 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net { 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The maximum number of bytes used by any internal mime sniffing routine. May 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be useful for callers to determine an efficient buffer size to pass to 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |SniffMimeType|. 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This must be updated if any internal sniffing routine needs more bytes. 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kMaxBytesToSniff = 1024; 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Examine the URL and the mime_type and decide whether we should sniff a 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// replacement mime type from the content. 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @param url The URL from which we obtained the content. 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @param mime_type The current mime type, e.g. from the Content-Type header. 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @return Returns true if we should sniff the mime type. 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NET_EXPORT bool ShouldSniffMimeType(const GURL& url, 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& mime_type); 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Guess a mime type from the first few bytes of content an its URL. Always 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// assigns |result| with its best guess of a mime type. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @param content A buffer containing the bytes to sniff. 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @param content_size The number of bytes in the |content| buffer. 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @param url The URL from which we obtained this content. 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @param type_hint The current mime type, e.g. from the Content-Type header. 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @param result Address at which to place the sniffed mime type. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// @return Returns true if we have enough content to guess the mime type. 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NET_EXPORT bool SniffMimeType(const char* content, size_t content_size, 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GURL& url, const std::string& type_hint, 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string* result); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Attempt to identify a MIME type from the first few bytes of content only. 45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Uses a bigger set of media file searches than |SniffMimeType()|. 46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// If finds a match, fills in |result| and returns true, 47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// otherwise returns false. 48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// The caller should understand the security ramifications of trusting 50a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// uncontrolled data before accepting the results of this function. 51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// 52a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// @param content A buffer containing the bytes to sniff. 53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// @param content_size The number of bytes in the |content| buffer. 54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// @param result Address at which to place the sniffed mime type. 55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// @return Returns true if a MIME type match was found. 56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)NET_EXPORT bool SniffMimeTypeFromLocalData(const char* content, 57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) size_t content_size, 58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) std::string* result); 59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace net 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // NET_BASE_MIME_SNIFFER_H__ 63