I don't have experience in using c++ with apache or other web server. But now want to use it with yaws to generate pdf from passed data. May be somebody has experience in doing so. Please any link. I havent found any. May be better to not use executable file but library and call its functions from yaws module.
Since you included the cgi tag on your question, one way to do this is to use the Yaws CGI capabilities. You'd simply run your C++ program as a CGI program. Yaws also supports FCGI, which lets you avoid starting a new instance of your C++ program for every request and instead have a dedicated TCP connection between Yaws and a daemon instance of your program.
Another way to do this is to write a Yaws appmod exporting an out/1 function. The argument to this will be an #arg record detailing all the information of the request being served. Your out/1 function could then call into an Erlang NIF written in C++, passing whatever information from the request it needs, and then taking replies and giving them back to Yaws as responses. PDF data could be returned from your NIF as Erlang binaries. With this approach, your C++ code runs in the same OS process as Yaws, so you have to be careful that your code never crashes else it will take the whole Erlang VM down with it, but aside from that this approach would be more efficient than FCGI or CGI.
Related
I'm coding a web server for fun and I'm now trying to make it dynamic.
I need to do the part usually PHP would do in regular web development, be inside the page code and be executed server side while the server runs.
Now, I've been thinking, and reading a bit on this. I could separate it and then invoke the compiler to make that code run, but I've also read that's not a good way to go about it, so I'd like to avoid it.
I thought maybe if I compiled a program that would go with the specific page it was meant to and made it change the page based on arguments and return the page in buffers, it could work, but that just seems cumbersome.
One wouldn't want to include the code for every single page on the webserver because it just doesn't scale well.
I don't believe C++ can do a "CALL" as in batch when the program is running, it would though, I believe, be the ideal solution besides including a file mid program which isn't possible.
I would like to know whether or not there's a way of executing a program or non-included code during runtime and if so how.
You could try dlopen, dlsym, dlerror and dlclose. These allow you to, at runtime, load libraries. Your server could use it to load code which should execute inside a page.
Howto, by Aaron Isotton
Documentation on man7.org
I have a program written in C++ with a web interface to for the purpose of RPC. I can call http://localhost/ListVariables or http://localhost/RunFunction?var=1 and have the C code execute ListVariables or RunFunction. It works, but I'd rather not have to manage the web server in C/C++ when there are so many good Python web servers out there.
What I'm imagining is having the C program call into Python to start a web server on another thread (i.e. Tornado), return to C and then continue chugging along doing calculations. Then when the Python server receives a request on http://localhost/ListVariables, it calls back into C and executes ListVariables on the already running process.
C -----> processing -----------> processing ------------> RPC: FuncA -------->
| ^ |
\---> Python Web Server ---------- Request for: FuncA --/ ... \-------->
^
browser: http://localhost/FuncA ---/
The project has the unfortunate caveat that the program must be started from C to begin with. After doing some research this seems to be a bit of a border case, since Python-->C and C-->Python can be done with Cython. However, I can't find many resources on C-->Python-->C, as most of the examples I've found describe linking to libraries and not to already-running processes. Is it possible to have Python call back into a running C program?
Absolutely. Create a "fake" module in the C program injected into sys.modules that the Python code can import and access as it would any other module.
I personally really like the boost::python library for embedded python into c++ and dealing with data binding and callbacks between the two. http://www.boost.org/doc/libs/1_51_0/libs/python/doc/
That being said, what you're proposing sounds like a bit of a PITA. I've personally have had a lot of success using http://libevent.org/ to fulfill my embedded webserver needs. It handles all of the http server stuff for you, all you need to do is give it the urls you want to handle and a callback, put the event_base_loop somewhere in your main loop (or in it's own thread if you'd rather), and away you go. That to me seems like it might be a lot easier than embedding python and dealing with passing data and data structures between the two.
I have a Windows local service that may spawn off a process to execute a JScript script (in a .js file) via the Windows Script Host. The issue is that I need to notify my service of the results generated by the script in the .js file. A transfer or a simple 32-bit integer, or a string would suffice. What would be the way to do this?
PS. The code must run on Windows XP SP3 or later OS.
Your best bet is to create an out of process COM object that executes within your service. Just implement the necessary scripting interfaces and provide a member function to match the notification and call it from your script as such:
newObj = new ActiveXObject("localserver.mynotify");
newObj.Notify("finished");
Would the exit code of the process be enough?
Windows Scripting host has has a .Quit(errorCode) method that allows you to set the exit code.
You should be able to call WSH directly from the service and get the return code with GetProcessExitCode() by passing the process handle that you received after spawning it.
Note that almost everything you can do from a JScript file can also be done with native code.
Do you have to execute the .js file as an external process? Windows Scripting has COM objects that an app can use to run scripts within its own process. I use this to execute script files within my service processes, and it works fine. The hosting process can even implement its own IDispatch-based classes and pass them to the scripting engine to expose to scripts as global objects so the scripts can communicate with the hosting process without having to use new ActiveXObject or CreateObject() to access those objects.
I see your script is written in JScript and your app in C++.
Perhaps the easiest way to accomplish what you want is by writing a file, say, to programdata folder which your service should have access to. Maybe use a GUID for the particular request, pass that to the JScript so it's guaranteed to be a unique file. Not ideal.
Another way to get JScript output ... Can you call out to managed code (C#)? If so, you could use a .NET-based or .NET-callable JavaScript compiler/interpreter. This would allow you to avoid IActiveScript and also to grab the values right out of the script context or from function return. I've used Jurassic and JavaScriptDotNet, both very easy to use and extend.
This might open a problem if you heavily rely on ActiveXObject calls (ie: FileSystemObject) and don't want to write components. JuraScript wraps the Jurassic engine and add ActiveXObject support to it for COM automation.
I am a C++ newb, so I don't know how much of a leap this is for you although I know it's possible to interop between managed/C++.
Just thought I'd mention these scenarios as I didn't see them listed in answers.
is it possible to get the output of an c++ project as http request
eg:http://localhost:8080
just it need to output an xml or just any output...
Yes - this is typically some sort of CGI mechanism. Depending on the software you're using to run your webserver, if it supports CGI, it can be configured to invoke your program when certain URLs are requested. Your program's output to stdout would then be sent back to the HTTP client.
Be careful with this approach, especially if your application takes input from the user: bugs in your program can lead to security vulnerabilities if, for example, you have the possibility of a buffer overflow. Interpreted languages can sometimes offer some protection here.
You can use any executable as a CGI script (though they are usually perl or shell scripts).
Make your c++ executable print whatever you need to stdout (cout).
Here is a great into to CGI, and 4.2 tells you that you can make any executable run via CGI.
I'm starting up a new embedded system design using FreeRTOS. My last one used eCos, which has a built-in HTTP server that's really lightweight, especially since I didn't have a filesystem. The way it worked, in short, was that every page was a CGI-like C function that got called when needed by the HTTP daemon. Specifically, you would write a function of the form:
int MyWebPage(FILE* resp, const char* page, const char* params, void* uData);
where page was the page part of the url, params were any form parameters (only GET was supported, not POST, which prevented file uploads and thus made burning the flash a pain), uData is a token passed in that was set when you registered the function, so you could have the same function serve multiple URLs or ranges with different data, and resp is a file handle that you write the HTTP response (headers and all) out to.
Then you registered the function with:
CYG_HTTPD_TABLE_ENTRY(www_myPage, "/", MyWebPage, 0);
where CYG_HTTPD_TABLE_ENTRY is a macro where the first parameter was a variable name, the second was a page URL (the * wildcard is allowed; hence page getting passed to MyWebPage()), third is the function pointer, and last is the uData value.
So a simple example:
int HelloWorldPage(FILE* resp, const char*, const char* params, void*)
{
fprintf("Content-Type: text/html;\n\n");
fprintf("<html><head><title>Hello World!</title></head>\n");
fprintf("<body>\n");
fprintf("<h1>Hello, World!</h1>\n");
fprintf("<p>You passed in: %s</p>\n", params);
fprintf("</body></html>\n");
}
CYG_HTTPD_TABLE_ENTRY(www_hello, "/", HelloWorldPage, 0);
(Actually, params would be passed through a function to escape the HTML magic characters, and I'd use another couple functions to split the params and make a <ul> out of it, but I left that out for clarity.)
The server itself just ran as a task (i.e. thread) and didn't get in the way as long as it had a lower priority than the critical tasks.
Needless to say, having this proved invaluable for testing and debugging. (One problem with embedded work is that you generally can't toss up an XTerm to use as a log.) So when Supreme Programmer reflexively blamed me for something not working (path of least resistance, I guess), I could pull up the web page and show that he had sent me bad parameters. Saved a lot of debug time in integration.
So anyway... I'm wondering, is there something like this available as an independent library? Something that I can link in, register my callbacks, spawn a thread, and let it do the magic? Or do I need to crank out my own? I'd prefer C++, but can probably use a C library as well.
EDIT: Since I'm putting a bounty on it, I need to clarify that the library would need to be under an open-source license.
I suggest you have a look at libmicrohttpd, the embedded web server:
http://www.gnu.org/software/libmicrohttpd/
It is small and fast, has a simple C API, supports multithreading, is suitable for embedded systems, supports POST, optionally supports SSL/TLS, and is available under either the LGPL or eCos license (depending). I believe this fulfils all your requirements. It would be trivial to wrapper in C++ if you preferred.
Mongoose is licensed under GPLv2 and is lightweight (just one C file so easy to include into a new project). It will run in a separate thread and support callbacks.
http://www.ibm.com/developerworks/systems/library/es-nweb/index.html
Seems exactly what you are after. You my need to do a small amount of re-writing to get it to run under FreeRTOS but its a very small, very lightweight web server.
I'm not familiar with FreeRTOS and how it supports TCP/IP and sockets, so I can't say for sure but you might want to take a look at the GoAhead web server. http://embedthis.com/goahead/