This chapter introduces the plug-in APIs that are responsible for creating and destroying streams both directly and indirectly. You will learn how to get valuable information from a new stream, such as the MIME type, last modification date, and whether it supports seeking with byte range requests. Stream instance structures are reviewed along with stream type return values.
Additionally, the APIs NPN_GetURL and NPN_PostURL, in addition to the new Navigator 3.0 APIs NPN_GetURLNotify, NPN_PostURLNotify, and NPP_URLNotify, are introduced with many code examples. You will learn how these APIs are also responsible for the creation of new data streams.
This chapter also introduces you to the following APIs:
NPP_NewStream
NPP_DestroyStream
NPN_NewStream
NPN_DestroyStream
NPN_GetURL
NPN_PostURL
NPN_GetURLNotify
NPN_PostURLNotify
NPP_URLNotify
The end of the chapter contains a quick reference with code examples for each of these APIs.
When the Navigator encounters a URL with a MIME type of your plug-in's registration, a stream is created to send the URL's data to your streaming routines. You are notified of a new stream with a call to your plug-in's API NPP_NewStream.
NPP_NewStream provides your code module with the parameters type, stream, seekable, and stype. These values contain important information concerning your stream.
The type value is a pointer to a buffer containing the MIME type that caused the creation of the stream. These can be one of the types that your plug-in registered with Netscape or a totally unrelated MIME type, perhaps originating from a call to NPN_GetURL. This type might not match the MIME type sent to a new plug-in instance via the NPP_New call.
| Warning |
In Navigator 2.x, if the stream results from a call to GetURL, the type field might be invalid. (It will return the type of the stream that launched the plug-in.) This has been fixed in Atlas (Navigator 3.x). |
A stream instance structure is passed to you with the stream parameter. This is a pointer to an NPStream structure currently defined as follows:
typedef struct _NPStream
{
void* pdata;
void* ndata;
const char* url;
uint32 end;
uint32 lastmodified;
} NPStream;
Within this structure, you notice some familiar friends: pdata and ndata. ndata points to the Navigator's instance data, and pdata is an extra pointer for your plug-in's use in streaming. For example, you might want to allocate an object and attach it to pdata so that your plug-in can easily manage a given stream. This technique is especially useful in tracking multiple streams within a plug-in.
stream->pdata = new MyStreamTrackingObject;
Also note url, end, and lastmodified. The url member tells you which URL is responsible for the creation of this stream. This can be handy for requesting more URLs via NPN_GetURL. The end member gives you the size of the requested stream. The lastmodified member indicates when the file associated with the URL was last modified.
| Warning |
Navigator gives as much information about the stream as it knows during a call to NPP_NewStream. When the stream is just beginning, it might not have received the size information yet. When the stream is generated on-the-fly by a CGI program, Navigator might not know how big the stream is going to be until it's actually finished. Therefore, you should be careful when using the end member of the NPStream structure. |
The seekable parameter of NPP_NewStream indicates whether the stream supports seeks or byte range requests. Byte range requests are performed through the use of NPN_RequestRead, which is documented in later chapters. Unfortunately, requesting a random block of a file is very new to the world of Web servers. At the time of this writing, most servers do not support it. You can, however, seek within a file that is already on the local system. Additionally, a stream is seekable if it has previously been cached on the local system.
Speaking of local files, you might be interested in the stype parameter. This is a pointer to a value that is filled with the requested mode of streaming. The values are as follows:
| Note |
Netscape recommends that plug-ins use a full streaming technique whenever possible. This is especially true for files located on Internet servers. Users might not want to wait for the full file download, as in NP_ASFILE. |
When a stream is complete or interrupted, Netscape makes a call to your NPP_DestroyStream API. Although it is called in streams of type NP_ASFILE, this API is really only relevant to plug-ins that process data in a streaming fashion. Plug-ins that are file based are notified to begin processing with a call to NPP_StreamAsFile.
If you are writing a streaming-based plug-in, you should pay close attention to NPP_DestroyStream. For example, if your plug-in is processing data buffers as they come across the net and a user stops the transfer, the plug-in needs to know that no more data is coming. File-based plug-ins don't care because they don't begin processing until the file is fully downloaded.
NPP_DestroyStream contains three parameters: instance, stream, and reason. The instance structure is the same as with most APIs. The stream structure is identical to when it left NPP_NewStream. The reason parameter tells you why the stream is being destroyed.
The following are possible reasons for stream destruction:
NPRES_NETWORK_ERR
NPRES_USER_BREAK
NPRES_DONE
NPRES_NETWORK_ERR, as the name says, is a network error. This could be caused by someone picking up a phone during a modem connection. NPRES_USER_BREAK is caused by the user aborting the streaming process by pressing the Stop button, going to another link, or even ending the application. NPRES_DONE indicates that the file has been fully transferred.
| Warning |
Netscape 2.x SDK release notes state that the reason is wrong in some cases. This bug might have been fixed in later releases of 2.x, but you should be wary of reason. |
With the release of Netscape's Navigator 3.0 plug-in API comes the methods NPN_NewStream, NPN_DestroyStream, and NPN_Write. These methods were present in the 2.0 API release, but they did not work at that time. You can use these APIs to create and manage a data stream from your plug-in to the Navigator. One good application would be using a plug-in as a filter.
Chapter 19, "The CPU Monitor Plug-In," gives an example of a plug-in running a CGI program through the use of NPN_GetURL. This program runs a UNIX command called vmstat and gets the results of the command from a Navigator-created data stream. The plug-in then displays this data with a bar graph within the plug-in's window. You can use part of this technique to make your plug-in act as a filter for the Navigator.
For the sake of this discussion, assume that your boss wants to view UNIX-style man pages within Netscape's Web Broswer. Your plug-in can run a CGI program as in the CPU Monitor plug-in that runs man with a given command. After Navigator streams back the results of the CGI program, your plug-in can buffer this data and create a stream to Netscape's Navigator. The plug-in acts as a filter because it can take raw man text and convert it to formatted HTML for display by the Navigator. The following code shows some highlights for using this technique:
//
// Send HTML formatted data to the Navigator
//
.
.
.
// Allocate and clear an NPStream structure
NPStream stream;
memset (&stream, 0, sizeof(NPStream));
// Create a new stream of MIME type "text/html"
// and target a blank web page
NPError rc = NPN_NewStream (instance, "text/html", "_blank", &stream);
// Write the previously formatted HTML data to the Navigator
bytesWritten = NPN_Write (instance, &stream, htmlLen, htmlBuffer);
.
.
.
// Destroy the stream when finished
rc = NPN_DestroyStream (instance, &stream, NP_RESDONE);
At the time of this writing, Navigator 3.0 is still in beta and the APIs as shown in the preceding code block are not yet fully functional. Because of this, you won't find any working code examples of a plug-in acting as a filter in this book.
Both NPN_GetURL and NPN_PostURL are resolved to HTTP server methods by the Navigator. Server methods indicate what to do with the object referenced by the given URL. As the names imply, NPN_GetURL resolves to a GET and NPN_PostURL resolves to a POST. (If you want to read up on other HTTP methods, a good place to start is The World Wide Web Consortium at http://www.w3.org.)
The main difference between GET and POST is that GET is used mostly for requesting URL objects, while POST is the preferred method of sending data to a server. That doesn't mean GET is never used to send data. In fact, many HTML programs embed data within the URL in the form of name=value pairs. For example, a Yahoo search for the string "Netscape" yields the following:
http://search.yahoo.com/bin/search?p=Netscape
Although GET is restricted to inserting data directly in the URL string, POST is more advanced and appends data after the POST header.
The GET server method is used for most requests for URLs. For example, if you type http://www.yahoo.com in your Web browser, the browser sends the following GET request to the HTTP server:
GET / HTTP/1.0
If-Modified-Since: Monday, 25-Mar-96 23:48:32 GMT; length=5683
Connection: Keep-Alive
User-Agent: Mozilla/2.01 (Win95; I)
Host: www.yahoo.com
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Although the specifics of this GET header are relevant to HTTP syntax and beyond the scope of this book, you can get a good idea of what the browser is looking for by reading the text.
After the GET request is sent, the browser waits patiently for word from the server. In the case of Yahoo, you end up with the following:
HTTP/1.0 200 OK
Last-Modified: Wed, 27 Mar 1996 09:33:06 GMT
Content-Type: text/html
Content-Length: 5688
<HTML>
.
.
.
</HTML>
Reading through the text, you can see the last modification date,
content type, and content length. The actual HTML code has been
removed for the sake of brevity. Within the HTML code are mode
URL objects such as graphics, which cause your browser to issue
more GET commands.
| Tip |
If want to see what is sent back and forth through a Windows 95 socket connection, check out WSOCK00.DLL and its associated source code on this book's CD-ROM. This socket spy utility dumps data files corresponding to socket transfers to your local hard drive. Chapter 24, "Debugging with the Socket Spy," details how to use this utility. |
The POST HTTP method is the preferred method for sending data to HTTP servers. Send- ing data to a server in this manner requires a server script or program using the Common Gateway Interface (CGI). A CGI program lives in a special directory that is set up by the server administrator. If you don't have access to a server, check out the National Center for Supercomputing Applications (NCSA) Fill-Out Form Support Page, which is currently located at the following address:
http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html
You can test your programming skills using Netscape's NPN_PostURL by emulating one of the NCSA sample forms. For example, take a look at the form shown in Figure 10.1.
Figure 10.1 : An example form that passts data to a CGI program from NCSA.
If you view the HTML source for this example, you see the following simple FORM defined:
<FORM METHOD="POST" ACTION="http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query">
A single text entry field goes here: <INPUT NAME="entry"> <P>
Note that it has no default value. <P>
To submit the query, press this button: <INPUT TYPE="submit"
VALUE="Submit Query">. <P>
</FORM>
Notice FORM METHOD="POST", which corresponds to an HTTP POST. Also notice ACTION="http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query". The ACTION points to a URL containing the CGI program to process the FORM.
When a user clicks on the Submit Query button, the following POST is sent to the CGI program post-query:
POST /cgi-bin/post-query HTTP/1.0
Referer: http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/
Âfill-out-forms/example-1.html
Connection: Keep-Alive
User-Agent: Mozilla/2.01 (Win95; I)
Host: hoohoo.ncsa.uiuc.edu
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Content-type: application/x-www-form-urlencoded
Content-length: 13
entry=My+Name
The post-query program is written in C and can be found at the following ftp site:
ftp://ftp.ncsa.uiuc.edu/Web/httpd/Unix/ncsa_httpd/cgi/cgi-src/post-query.c
Take a look at the post-query.c source code from NCSA:
#include <stdio.h>
#define MAX_ENTRIES 10000
typedef struct {
char *name;
char *val;
} entry;
char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);
main(int argc, char *argv[]) {
entry entries[MAX_ENTRIES];
register int x,m=0;
int cl;
printf("Content-type: text/html%c%c",10,10);
if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
printf("This script should be referenced with a METHOD of POST.\n");
printf("If you don't understand this, see this ");
printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/
Âfill-out-forms/overview.html\">forms overview</A>.%c",10);
exit(1);
}
if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) {
printf("This script can only be used to decode form results. \n");
exit(1);
}
cl = atoi(getenv("CONTENT_LENGTH"));
for(x=0;cl && (!feof(stdin));x++) {
m=x;
entries[x].val = fmakeword(stdin,'&',&cl);
plustospace(entries[x].val);
unescape_url(entries[x].val);
entries[x].name = makeword(entries[x].val,'=');
}
printf("<H1>Query Results</H1>");
printf("You submitted the following name/value pairs:<p>%c",10);
printf("<ul>%c",10);
for(x=0; x <= m; x++)
printf("<li> <code>%s = %s</code>%c",entries[x].name,
entries[x].val,10);
printf("</ul>%c",10);
}
This CGI program is the back-end of the form shown in Figure 10.1. Here are some things to note:
You can produce the same output as the form in Figure 10.1 through the use of NPN_PostURL. Try the following in a test plug-in:
CString csPost = "Content-type: application/x-www-form-urlencoded\n";
csPost += "Content-length: 13\n\n";
csPost += "Entry=My+Name\n";
// Send data to the server
NPError rc = NPN_PostURL (instance,
"http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query",
"_blank",
csPost.GetLength(),
csPost.GetBuffer(csPost.GetLength()),
TRUE);
Figure 10.2 : NCSA's CGI program responds to a cal from NPN_PostURL.
If, by the time you are reading this book, NCSA's example has
gone away, simply find yourself another FORM
example that uses the POST
method. Or, if you are lucky enough to have access to an HTTP
server, just write your own CGI program.
| Warning |
You cannot use a buffer with NPN_PostURL for Netscape Navigator version 2.x, as the previous example suggests. If you do, because of a bug in this version, an extra blank line is appended to the end of the Accept: line. The previous example's POST request would look like this: |
POST /cgi-bin/post-query HTTP/1.0 |
HTTP servers are very sensitive to new line placement and will not work in this case. The solution is to use a temporary file instead of a buffer. The code example for the NPN_PostURL API at the end of this chapter shows how to do this. |
Both NPN_GetURL and NPN_PostURL provide a means for targeting windows. Target windows are windows within the Navigator that receive the data stream associated with a given URL. In most cases, you use the value of NULL for your window target. Using NULL ensures that the data stream is written to your plug-in via NPP_Write. Otherwise, you might specify a number of target windows relating to the TARGET tag within your HTML code or use one of the "magic" target window names.
The following are the current magic target windows:
_blank
_new
_self
_current
_parent
_top
Of these, _blank and _new are synonymous and cause the Navigator to create a new unnamed window instance of itself. The targets _self and _current are also synonyms. They cause the link to load in the same window in which the anchor was clicked.
The target _parent loads the link in the immediate FRAMESET parent of the document, and _top makes the link load in the full body of the window.
For more information on frames and magic window names from Netscape, please consult the following addresses:
http://home.netscape.com/assist/net sites/frames.html
http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/targets/html
| Warning |
Although target windows work well with NPN_GetURL in Netscape Navigator 2.x, they are totally unreliable with NPN_PostURL. Use them at your own risk! |
The following sections give in-depth information on the APIs used in this chapter.
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT |
| API Type | Plug-in routine called from the Navigator. |
| Purpose | Notifies your plug-in instance of a new stream. |
| Syntax | NPError NPP_NewStream (NPP instance,
NPMIMEType type, NPStream *stream, NPBool seekable, uint16 *stype) |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. | |
| NPMIMEType type: A character pointer to the MIME type that caused the creation of the stream. | |
| NPStream* stream: A pointer to structure NPStream that references both the plug-in's and Netscape's stream instance data. This structure also contains the name of the URL that created this stream, its last modification date, and the content length in bytes. | |
| NPBool seekable: Indicates whether the stream is seekable. A value of TRUE is considered seekable. | |
| uint16* stype: The stream instance can request a new stream mode by setting this type. The current types are as follows: | |
| NP_NORMAL: Normal stream. This is the default. | |
| P_SEEK: Request a seekable stream. Actual seeking depends on your server. | |
| P_ASFILE: Request that the data is sent as a whole file rather than a stream. This mode is preserved for compatibility with the 2.0 API. For Navigator 3.0, use NP_ASFILEONLY. | |
| P_ASFILEONLY: New for Navigator API 3.0. This mode is more efficient than the older NP_ASFILE. If the file is local, it is not streamed and no calls to NPP_WriteReady and NPP_Write are made. | |
| Includes | #include <npapi.h> |
| Description | NPP_NewStream is a routine within your plug-in that is called by the Navigator to inform you of a new stream. This stream might have been created by a user action or a plug-in call such as NPN_GetURL. |
| As with most plug-in APIs, a pointer to your instance data is included with a call to NPP_NewStream. Similar to this, a pointer to a stream instance is also included in the call. You can attach your plug-in's per stream private data to the Navigator-supplied stream->pdata pointer. | |
| Other information such as MIME type, URL, last modification date, content length in bytes, and seekability are provided for your plug-in during this call. | |
| You can indicate how the stream should be handled with the stype parameter. Current types are normal streaming (NP_NORMAL), full file download (NP_ASFILE), or seeking (NP_SEEK). | |
| Returns | NPERR_NO_ERROR: No error.
NPERR_GENERIC_ERROR: An unknown error occurred. NPERR_INVALID_INSTANCE_ERROR: The instance value is invalid. NPERR_OUT_OF_MEMORY_ERROR: Out of memory. |
| See Also | NPP_DestroyStream, NPN_NewStream, NPN_GetURL |
| Example | #include <npapi.h>
// // A new stream has been created - check it out. // NPError NP_LOADDS NPP_NewStream (NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, uint16 *stype) { // Check for spurious calls from Netscape if (instance == NULL) return NPERR_INVALID_INSTANCE_ERROR; // Retrieve instance data MyInstance* myinstance = (MyInstance *)instance->pdata; // Take any action during this time if (myinstance->pObject) myinstance->pObject->NewStream (stream->url); return NPERR_NO_ERROR; } |
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT |
| API Type | Plug-in routine called from the Navigator. |
| Purpose | Notifies your plug-in instance that a stream is being destroyed. Usually called when a stream is complete. |
| Syntax | NPError NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason) |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. | |
| NPStream* stream: A pointer to structure NPStream that references both the plug-in's and Netscape's stream instance data. This structure also contains the name of the URL that created this stream, its last modification date, and the content length in bytes. | |
| NPError reason: A parameter indicating the reason why the stream was destroyed, which can be one of the following: | |
| NPRES_NETWORK_ERR: A network error occurred.
NPRES_USER_BREAK: The user aborted the stream NPRES_DONE: The stream finished normally. | |
| Includes | #include <npapi.h> |
| Description | Navigator calls NPP_DestroyStream when a stream has been closed and deleted by either a network error, a user action, or normal completion. |
| A streaming-based plug-in would be notified that the stream is complete and take any appropriate action. The parameter end in the NPStream structure contains the content length that is returned from the server. Usually, content length corresponds to the file size on the server. | |
| NP_ASFILE streams still get a call to NPP_DestroyStream after the call to NPP_StreamAsFile. | |
| Returns | NPERR_NO_ERROR: No error.
NPERR_GENERIC_ERROR: An unknown error occurred. NPERR_INVALID_INSTANCE_ERROR: The instance value is invalid. NPERR_OUT_OF_MEMORY_ERROR: Out of memory. |
| See Also | NPP_NewStream, NPN_DestroyStream |
| Example | #include <npapi.h>
// // A stream was destroyed. // NPError NP_LOADDS NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason) { // Check for spurious calls from Netscape if (instance == NULL) return NPERR_INVALID_INSTANCE_ERROR; // Retrieve instance data MyInstance* myinstance = (MyInstance *)instance->pdata; // Take any action during this time if (myinstance->pObject) myinstance->pObject->EndofStream (stream->url, reason); return NPERR_NO_ERROR; } |
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT. Navigator 3.0 and above only. |
| API Type | Navigator routine called from the plug-in. |
| Purpose | Creates a new stream for streaming data from the plug-in to the Navigator. |
| Syntax | NPError NPN_NewStream (NPP instance,
NPMIMEType type const char* target, NPStream* stream) |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. | |
| NPMIMEType type: A character pointer to the MIME type associated with this new stream. | |
| const char* target: The target window or frame. See the description for NPN_GetURL later in this chapter for target names and definitions. | |
| NPStream* stream: A pointer to structure NPStream that references both the plug-in's and Netscape's stream instance data. This structure also contains the URL, last modification date, and content length in bytes. | |
| Includes | #include <npapi.h> |
| Description | The NPN_NewStream API allows your plug-in to create a stream for streaming data from your plug-in to the Navigator. |
| Returns | NPERR_NO_ERROR: No error.
NPERR_GENERIC_ERROR: An unknown error occurred. NPERR_INVALID_INSTANCE_ERROR: The instance value is invalid. NPERR_OUT_OF_MEMORY_ERROR: Out of memory. |
| See Also | NPP_NewStream, NPN_DestroyStream |
| Example | #include <npapi.h>
. . . // // Create a new stream // NPStream stream; memset (&stream, 0, sizeof(NPStream)); // Create a new stream of MIME type "text/html" // and target a blank web page NPError rc = NPN_NewStream (instance, "text/html", "_blank", Â&stream); . . . |
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT. Navigator 3.0 and above only. |
| API Type | Navigator routine called from the plug-in. |
| Purpose | Closes and deletes a stream previously created with NPN_NewStream. |
| Syntax | NPError NPN_DestroyStream (NPP instance, NPStream* stream, ÂNPError reason) |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. | |
| NPStream* stream: A pointer to structure NPStream that references both the plug-in's and Netscape's stream instance data. This structure also contains the URL, last modification date, and content length in bytes. | |
| NPError reason: A parameter indicating the reason why the stream was destroyed, which can be one of the following:
NPRES_NETWORK_ERR: A network error occurred. PRES_USER_BREAK: The user aborted the stream. NPRES_DONE: The stream finished normally. | |
| Includes | #include <npapi.h> |
| Description | The NPN_DestroyStream method is new starting with Naviagator 3.0. This API allows your plug-in to destroy a stream. |
| Returns | NPERR_NO_ERROR: No error.
NPERR_GENERIC_ERROR: An unknown error occurred. NPERR_INVALID_INSTANCE_ERROR: The instance value is invalid. NPERR_OUT_OF_MEMORY_ERROR: Out of memory. |
| See Also | NPP_DestroyStream, NPN_NewStream |
| Example | #include <npapi.h>
. . . // // Destroy a stream // // Set the reason reason = NPRES_USER_BREAK; // Call the Navigator to destroy the stream NPError rc = NPN_DestroyStream (instance, stream, reason) . . . |
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT |
| API Type | Navigator routine called from the plug-in. |
| Purpose | Requests the creation of a new stream for the specified URL. This operation is asynchronous. |
| Sends a GET request to the Web server. | |
| Syntax | NPError NPN_GetURL (NPP instance, const char* url, const char* Âwindow) |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. This is the same instance value that is passed to every NPP_ call. | |
| const char* url: A pointer to a constant string containing the URL for the GET request. | |
| const char* window: A pointer to a user-supplied constant string indicating the display location of the stream. This parameter will be renamed target in future SDKs. See the following description for more information on this parameter. | |
| Includes | #include <npapi.h> |
| Description | Use NPN_GetURL to start a new stream of data to a specified window. |
| The window target should be NULL if you want the new stream to be passed to the current instance of your plug-in regardless of the new URL's MIME type. This allows your plug-in to read files of different MIME types such as text/html. | |
| You can specify the name of a target window for the new stream. This new stream may or may not be handled by your plug-in, depending on the MIME type and any magic target names. | |
| Window target names can be passed in as parameters to the plug-in with the EMBED tag. Magic target names can be used in the NPN_GetURL call, as in the following examples:
_blank creates a new blank Netscape window for the URL data. This window is not named. A synonym for this target is _new. _self causes the link to load in the same window. A synonym for this target is _current. _parent loads the link in the immediate FRAMESET parent of this document. It defaults to acting like _self if the document has no parent. _top makes the link load in the full body of the window. | |
| Returns | NPERR_NO_ERROR: No error.
NPERR_GENERIC_ERROR: An unknown error occurred. NPERR_INVALID_INSTANCE_ERROR: The instance value is invalid. NPERR_OUT_OF_MEMORY_ERROR: Out of memory. |
| See Also | NPN_PostURL, NPN_GetURLNotify, NPN_PostURLNotify, NPP_URLNotify |
| Example | #include <npapi.h>
. . . // // Get a URL // // Get the new URL and have the data streamed into the top most Âframe window NPError rc = NPN_GetURL (instance, "http://www.gate.net/ Â~zan/outofb.wav", "_top"); . . . |
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT |
| API Type | Navigator routine called from the plug-in. |
| Purpose | Use NPN_PostURL to send data to a URL. This API resolves to a POST request to a Web server. The routine is useful for sending data to a CGI script. |
| Syntax | NPError NPN_PostURL (NPP instance,
const char* url, const char* window, uint32 len, const char* buf, NPBool file) |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. | |
| const char* url: A pointer to a constant string containing the URL for the POST request. | |
| const char* window: A pointer to a user-supplied constant string indicating the display location of the stream. This parameter will be renamed target in future SDKs. | |
| uint32 len: Corresponds to the length of the following data buffer. | |
| const char* buf: A constant pointer to a buffer with the data for posting. | |
| NPBool file: If this Boolean is set to TRUE, the previous buffer contains a local filename for the Navigator to read instead of a data buffer. If it is FALSE, the buffer contains raw data. | |
| Includes | #include <npapi.h> |
| Description | NPN_PostURL sends a number of bytes to a URL with the server command POST. |
| If file is set to TRUE, a local temporary file is used to hold the post data. This file is deleted by Netscape after the post. If file is set to FALSE, a raw data buffer is used. | |
| Whether you use a local file or a raw data buffer, the format is specific to a POST command, as in the following example: | |
| char buf[] = "Content-type: application/x-www-form-Âurlencoded\nContent-length: 27\n\nName=John+Doe&Data=whatever\n"; | |
| Be sure to check the beginning of this chapter for problems with this API in Navigator 2.x. | |
| The window target should be NULL if you want the new stream to be passed to the current instance of your plug-in regardless of the new URL's MIME type. This allows your plug-in to read files of different MIME types such as HTML. | |
| You can specify the name of a target window for the new stream. This new stream may or may not be handled by your plug-in, depending on the MIME type and any magic target names. | |
| Window target names can be passed in as parameters to the plug-in with the EMBED tag. Magic target names can be used in the NPN_PostURL call, as in the following examples:
_blank creates a new blank Netscape window for the URL data. This window is not named. A synonym for this target is _new. _self causes the link to load in the same window. A synonym for this target is _current. _parent loads the link in the immediate FRAMESET parent of this document. It defaults to acting like _self if the document has no parent. _top makes the link load in the full body of the window. | |
| Returns | NPERR_NO_ERROR: No error.
NPERR_GENERIC_ERROR: An unknown error occurred. NPERR_INVALID_INSTANCE_ERROR: The instance value is invalid. NPERR_OUT_OF_MEMORY_ERROR: Out of memory. |
| See Also | NPN_GetURL, NPN_GetURLNotify, NPN_PostURLNotify, NPP_URLNotify |
| Example | #include <npapi.h>
. . . // // Post data to a URL using the file technique // // Setup some strings CString csFile = "posturl.txt"; CString csPost = "Content-type: application/x-www-form-Âurlencoded\n"; csPost += "Content-length: 18\n\n"; csPost += "Entry=Zan+Oliphant\n"; // Create a temp. file and write to it Cfile TmpFile (csFile.GetBuffer(csFile.GetLength()), CFile::modeCreate | CFile::modeReadWrite | ÂCFile::shareDenyNone); TmpFile.Write (csPost.GetBuffer(csPost.GetLength()), csPost.GetLength()); TmpFile.Close(); // Send data to the server NPError rc = NPN_PostURL (instance, "http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query", NULL, csFile.GetLength(), csFile.GetBuffer(csFile.GetLength()), TRUE); . . . |
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT, Navigator 3.0 and above. |
| API Type | Navigator routine called from the plug-in. |
| Purpose | Requests the creation of a new stream for the specified URL and gets a result notification through a future call to NPP_URLNotify. This operation is asynchronous. |
| Sends a GET request to the Web server. | |
| Syntax | NPError NPN_GetURLNotify (NPP instance,
const char* url, const char* target, void* notifyData); |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. | |
| Const char* url: A pointer to a constant string containing the URL for the GET request. | |
| Const char* target: A pointer to a user-supplied constant string indicating the display location of the stream. | |
| Void* notifyData: A plug-in private value that can be used to uniquely identify the request. This will be returned to the plug-in as a parameter to the corresponding NPP_URLNotify call. | |
| Includes | #include <npapi.h> |
| Description | The NPN_GetURLNotify API has the identical function as NPN_GetURL, but it provides for a result notification with a future call to NPP_URLNotify. |
| In order for your plug-in to correctly associate a NPN_GetURLNotify with the proper NPP_URLNotify, a new parameter called notifyData has been added to this API. You can attach a data block to this void pointer such as a pointer to an object or stream. | |
| See the previous reference on NPN_GetURL for the base functionality of NPN_GetURLNotify. | |
| Returns | NPERR_NO_ERROR: No error.
NPERR_GENERIC_ERROR: An unknown error occurred. NPERR_INVALID_INSTANCE_ERROR: The instance value is invalid. NPERR_OUT_OF_MEMORY_ERROR: Out of memory. |
| See Also | NPN_GetURL, NPN_PostURL, NPN_PostURLNotify, NPP_URLNotify |
| Example | #include <npapi.h>
. . . // // Get a URL with a result notification // // Get the new URL and have the data streamed into // the existing plug-in instance by using NULL for // the target. NPError rc = NPN_GetURLNotify (instance, "http://www.gate.net/~zan/outofb.wav", NULL, pMyPrivateData); . . . |
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT, Navigator 3.0 and above. |
| API Type | Navigator routine called from the plug-in. |
| Purpose | Use NPN_PostURL to send data to a URL with a result notification sent back to your plug-in with NPP_URLNotify. This API resolves to a POST request to a Web server. The method is useful for sending data to a CGI script. |
| Syntax | NPError NPN_PostURL (NPP instance,
const char* url, const char* target, uint32 len, const char* buf, NPBool file, void* notifyData); |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. | |
| const char* url: A pointer to a constant string containing the URL for the POST request. | |
| const char* window: A pointer to a user-supplied constant string indicating the display location of the stream. This parameter will be renamed target in future SDKs. | |
| uint32 len: Corresponds to the length of the following data buffer. | |
| const char* buf: A constant pointer to a buffer with the data for posting. | |
| NPBool file: If this Boolean is set to TRUE, the previous buffer contains a local filename for the Navigator to read instead of a data buffer. If it is FALSE, the buffer contains raw data. | |
| void* notifyData: This is a plug-in private value that can be used to uniquely identify the request. It is returned to the plug-in as a parameter to the corresponding NPP_URLNotify call. | |
| Includes | #include <npapi.h> |
| Description | The NPN_PostURLNotify API has the same function as NPN_PostURL, but provides for a result notification with a future call to NPP_URLNotify. |
| See the previous reference on NPN_PostURL for the base functionality of NPN_PostURLNotify. | |
| Returns | NPERR_NO_ERROR: No error.
NPERR_GENERIC_ERROR: An unknown error occurred. NPERR_INVALID_INSTANCE_ERROR: The instance value is invalid. NPERR_OUT_OF_MEMORY_ERROR: Out of memory. |
| See Also | NPN_GetURL, NPN_PostURL, NPN_GetURLNotify, NPP_URLNotify |
| Example | #include <npapi.h>
. . . // // Post data to a URL // // Setup some strings CString csPost = "Content-type: application/x-www-form-Âurlencoded\n"; csPost += "Content-length: 18\n\n"; csPost += "Entry=Zan+Oliphant\n"; // Send data to the server NPError rc = NPN_PostURL (instance, "http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query", NULL, csPost.GetLength(), csPost.GetBuffer(csPost.GetLength()), FALSE, pMyPrivateData); . . . |
| Compatibility | UNIX, Macintosh, Windows 3.1/95/NT, Navigator 3.0 and above. |
| API Type | Plug-in method called from the Navigator. |
| Purpose | Notifies your plug-in of the completion of a URL request through either NPN_GetURLNotify or NPN_PostURLNotify. |
| Syntax | void NPP_URLNotify (NPP instance,
const char* url, NPReason reason, void* notifyData); |
| NPP instance: A pointer to structure NPP that references both the plug-in's and Netscape's private instance data. | |
| const char* url: A pointer to a constant string containing the URL that was requested. | |
| void* notifyData: A plug-in private value that can be used to uniquely identify the request. This was previously set by the plug-in during either NPN_GetURLNotify or NPN_PostURLNotify. | |
| Includes | #include <npapi.h> |
| Description | The NPP_URLNotify method is called when the Navigator completes a NPN_GetURLNotify or NPN_PostURLNotify request to inform the plug-in that the request has completed for the reason specified by reason. |
The following values are currently defined for reason:
| |
| Returns | None. |
| See Also | NPN_GetURLNotify, NPN_PostURLNotify |
| Example | #include <npapi.h>
// // Process the URL notification // void NPP_URLNotify (NPP instance, const char* url, NPReason reason, void* notifyData); { // Check for spurious calls from Netscape if (instance == NULL) return NPERR_INVALID_INSTANCE_ERROR; // Retrieve instance data MyInstance* myinstance = (MyInstance *)instance->pdata; // If the reason indicates no error, process if (reason == NPRES_DONE) { myinstance->URLRequestComplete (url, notifyData); } return NPERR_NO_ERROR; } |
This chapter showed you how to create and destroy streams both implicitly and explicitly. Data structures were defined and parameters both to and from the Navigator were covered. Special attention was given to sending data to HTTP servers with NPN_PostURL.
Make sure to take note of the warnings about use of these APIs. Netscape Navigator 2.x has a few bugs and does not support some of the streaming APIs.
Now that you're an expert at creating streams, it's time to move on to reading and writing to them. In the next chapter, you will see how both NPP and NPN type APIs enable you to move data to and from the server. The next chapter covers the APIs NPP_StreamAsFile, NPP_WriteReady, NPP_Write, NPN_RequestRead, and NPN_Write.